admin管理员组文章数量:1435859
I am setting up a React application that extracts from a public API. When I am testing the API in Postman, it works without problems. But if I use fetch()
in my React app, I get a CORS error.
As far as I have understood, my client request should be allowed on the server using headers. But I don't own the server hosting the API, so I cannot do anything on the server. The API is configured to be used by third party applications using an access code, so I am a bit confused why it's not allowed to extract from the API in React.
What are my options?
I am setting up a React application that extracts from a public API. When I am testing the API in Postman, it works without problems. But if I use fetch()
in my React app, I get a CORS error.
As far as I have understood, my client request should be allowed on the server using headers. But I don't own the server hosting the API, so I cannot do anything on the server. The API is configured to be used by third party applications using an access code, so I am a bit confused why it's not allowed to extract from the API in React.
What are my options?
Share Improve this question asked Jul 7, 2018 at 9:54 mortensenmortensen 1,2372 gold badges14 silver badges24 bronze badges 2- You most likely have to use your own server as the middle man, so that the browser is only fetching data from the same origin. – Tholle Commented Jul 7, 2018 at 9:56
- what cors error is it so we cans set the correct headers? – Joe Warner Commented Jul 7, 2018 at 10:12
2 Answers
Reset to default 1if you don't have server access then you can bypass it
- CORS Anywhere: https://cors-anywhere.herokuapp./
- All Origins: http://allorigins.me/
- Any origin: http://anyorigin./
CORS is a security thing, under most circumstances the best practise would be not to try to allow the client to make callouts to wherever (the old clientside CORS * * * hackaroonie).
Instead, set up a simple serverside relay, as Tholle suggested above a middleman, in the same deployment (same origin).
There's a brief example of how you'd do this here.
本文标签: javascriptEnable CORS in React appStack Overflow
版权声明:本文标题:javascript - Enable CORS in React app - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745674665a2669778.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论