Requesting API with fetch in javascript has CORS policy error

后端 未结 1 1692
南笙
南笙 2021-01-27 13:48

I\'m trying to call some APIs with fetch in my javascript code. I\'m developing with ReactJs in my machine and have another development in the same network developing the API wi

相关标签:
1条回答
  • 2021-01-27 14:28

    When performing an API call from a web browser, it will often do something called a "preflight check". This is essentially where the browser sends a request to your API (ahead of your own API call), to check what it is allowed to do and whether it's even worth sending the actual request.

    To do this, it uses the OPTIONS method (instead of GET, POST .etc).

    If your API endpoint hasn't been configured to accept OPTIONS requests, then the preflight request from the browser will fail - and it will abort sending your request.

    To fix your issue, you need to configure your API to accept OPTIONS methods. You should also check which origins (IP addresses of clients connecting to the API) are allowed.

    0 讨论(0)
提交回复
热议问题