How to Fix react cors error in localhost?

大憨熊 提交于 2020-05-16 20:10:25

问题


I'm working on a react app. Where I'm requesting for an API by AXIOS. But When I run NPM START to test my app in localhost I'm getting CORS error. Here is the error Access to XMLHttpRequest at 'https://********.com/trx_status.php' from origin 'http://localhost:3000' has been blocked by CORS policy: Request header field privatekey is not allowed by Access-Control-Allow-Headers in preflight response.

I'm new in react. Please tell me how can I solve this issue. Thank you...


回答1:


The error is caused by the custom privatekey header that is send to the server. This field has to be included in the Access-Control-Allow-Headers response header from the server. It can be done using:

Access-Control-Allow-Headers: privatekey

when using php the following snippet can be used:

header('Access-Control-Allow-Headers: X-Requested-With, privatekey');



回答2:


This seems to me like an issue at your server side. So what you could try doing is to try adding the header "Access-Control-Allow-Origin: *".

It would be helpful if you could post it somewhere in jsfiddle or some editor so we can look at it further.

Thanks



来源:https://stackoverflow.com/questions/58679084/how-to-fix-react-cors-error-in-localhost

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!