Question

I am trying to do API request by using RESTful API, however, I got error message below from console.

XMLHttpRequest cannot load http://example.com/abcd. Response to preflight request doesn’t pass access control check: The ‘Access-Control-Allow-Origin’ header has a value ‘http://example.com' that is not equal to the supplied origin. Origin ‘http://example.com' is therefore not allowed access.

Answer

Add the following code into app.js.

1
2
3
4
5
6
app.use(function(req, res, next) {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
next();
})

Reference


This is the end of post