Access to XMLHttpRequest at '...' from origin 'localhost:3000' has been blocked by CORS policy
if you are building your rest api in nodejs. Follow the folowing simple steps
Stop the Node.js server.
npm install cors --save
Add following lines to your server.js or index.js
var cors = require('cors')app.use(cors()) // Use this after the variable declaration
Now try to make your api call on the client side and it should work
After many days finally I got a solution . Instead of using CORS simply like this
const cors = require('cors');
app.use(cors());
in your server index.js using CORS option will solve the issue and now you can pass cookies or other credentials
const cors = require('cors');const corsOptions ={ origin:'http://localhost:3000', credentials:true, //access-control-allow-credentials:true optionSuccessStatus:200}app.use(cors(corsOptions));
You can't really fetch data from servers, with a different hostname, that don't have a CORS policy to allow request from your domain.
In simpler words, localhost
can't call ipify.org
unless it allows it. It seems like it doesn't, and I assume that server is not managed by you. So your only option is to go with a reverse proxy. You can read how to create an http proxy with node here.
What you need is for your app to be served on a fake/stubbed host, rather than localhost:
local.development.ipify.org
-> proxies to localhost:3000
That way, when you make your api call, you are under the same domain as ipify.org
, and you won't get any CORS issues.