How to fix local dev CORS issues

I was getting CORS issues when trying to request my API:

Access to fetch at 'https://myapiurl.com/myapipath' from origin 'http://192.168.99.107:19006' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

On iOS/Android you don’t have to worry about CORS issues but on web you do. Here’s how I was able to fix it for my local dev environment, so that I could query my API:

Create a webpack.config.js file. You can do this by running expo customize:web.

Change the config to the following

const createExpoWebpackConfigAsync = require('@expo/webpack-config');

module.exports = async function(env, argv) {
  const config = await createExpoWebpackConfigAsync(env, argv);

  if (config.mode === 'development') {
    config.devServer.proxy = {
      '/**': {
        target: {
          host: 'example.com',
          protocol: 'https:',
          port: 443,
        },
        secure: false,
        changeOrigin: true,
        logLevel: 'info',
      },
    };
  }

  return config;
};

This won’t fix all CORS issues - there’ll still be issues with querying other people’s APIs - but at least you can now query your own APIs!

2 Likes

the same question

Hello I have the same problem but in produccion, how I can I do solve that.

I cant requests other people’s API, How can we solve it?

Access to XMLHttpRequest at 'https://billetera.paytoperu.com/login' from origin 'http://192.168.1.62:19006' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status.

The way to fix this problem consists of: Add the support of the OPTIONS method so that CORS preflight requests are valid.

Did you ever manage to solve it? I am running into this error message in dev, trying to access my own local server.

My solution was opening chrome without the CORS feature, which means chrome will not stop you from making CORS requests. Read how to do it here

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.