blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
CORS problem in browser /node js.
By default the browser is disabled the cross-origin-resources-share for the purpose of security problem. The browser try to maintain (block) the unwanted use of syncing resouces by other end.If you run this code in browser console which is given below
How to resolve problem "blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource."
If you are using google chrome browser then go to extension of google chrome and install "cors plugin". Link given below.
After installing "cors plugin" then "enabled it" now you request above code, Hence we not get any error of "blocked by cors policy".
Genuine rule to bypass the problem of "blocked by cors policy"
Simply go to function of request and response block and paste the given below code.
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
res.setHeader('Access-Control-Allow-Credentials', true);
Let's take example to use the "res.setHeader" method.
prerequisite install
npm i express
npm i body-parser
npm i path
now ready to run this code "node filename.js"
If you have a problem then comment below...@
ReplyDelete