javascript - Enable CORS in React app - Stack Overflow

I am setting up a React application that extracts from a public API. When I am testing the API in Postm

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
Add a ment  | 

2 Answers 2

Reset to default 1

if 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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745674666a4639613.html

相关推荐

  • javascript - Enable CORS in React app - Stack Overflow

    I am setting up a React application that extracts from a public API. When I am testing the API in Postm

    5小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信