I can request to a API very easily in Postman. But when I try it with React JS and on localhost:3000
, it throws me an error:
Please tell me why?! Thanks
See https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS/ If you know CORS problems, it would be easier to explain.
Because the requests sent by Postman or ones by your React.JS are kind of different. As you send requests with Postman directly, the requests are considered sent by as "a person", like you enter the URLs of your APIs in your browser.
It's different with sending requests with React because React is a front-end JavaScript based framework language "executed" by your browser. Imaging if you happen to access a malicious web-site, and the site sends intended codes of React(or some JavaScript) to manipulate your browser(An easy example: If there are no limits, it can use your browser as a web crawler to other sites).
You know that every time you open a site, your browser is executing lots of codes from the site.
So you may have to understand why we need CORS policy, and how CORS works to develop your APIs.