ppshein ppshein - 11 months ago 60
Node.js Question

Response to preflight request doesn't pass access control check in NodeJS

I cannot convince why I've encountered following error even I've added following code in NodeJS/ExpressJS.


XMLHttpRequest cannot load http://localhost:9000/polymer/105724/apply.
Response to preflight request doesn't pass access control check: No
'Access-Control-Allow-Origin' header is present on the requested
resource. Origin 'http://localhost:8080' is therefore not allowed
access.


res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
res.setHeader("Access-Control-Allow-Headers", "X-Requested-With, Content-Type");

Answer Source

You also need to allow OPTIONS method. Browser sends an OPTIONS call before the actual call so if OPTIONS is not allowed next call will not be made.

res.setHeader('Access-Control-Allow-Methods', 'OPTIONS,GET,PUT,POST,DELETE');