Thursday, March 14, 2019

CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource

Scenario :

My UI is developed in OJet. It accept some input fields from browser and these inputs needs to be passed as a parameter a shell script.

My Ojet application is running on port 80 and I have a nodejs server running on port 8000. From Ojet web UI when a user clicked on button, it will call an api POST request to my nodejs application and the user input fields are as parameters.

How to solve this error

CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

Open your nodejs server.js file (In my case) . Add the following res.header fileds.

app.use(function(req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
});

It fixed my issue.

No comments:

Post a Comment