[英]How to Fix react cors error in localhost?
I'm working on a react app.我正在开发一个反应应用程序。 Where I'm requesting for an API by AXIOS.我要求 AXIOS 提供 API。 But When I run NPM START to test my app in localhost I'm getting CORS error.但是当我运行 NPM START 在本地主机中测试我的应用程序时,我收到 CORS 错误。 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. 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 Access-Control-Allow-Headers 在预检响应中不允许。
I'm new in react.我是新来的反应。 Please tell me how can I solve this issue.请告诉我如何解决这个问题。 Thank you...谢谢...
The error is caused by the custom privatekey
header that is send to the server.该错误是由发送到服务器的自定义私钥privatekey
引起的。 This field has to be included in the Access-Control-Allow-Headers
response header from the server.该字段必须包含在来自服务器的Access-Control-Allow-Headers
响应 header 中。 It can be done using:可以使用以下方法完成:
Access-Control-Allow-Headers: privatekey
when using php the following snippet can be used:使用 php 时,可以使用以下代码段:
header('Access-Control-Allow-Headers: X-Requested-With, privatekey');
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: *".因此,您可以尝试添加 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.如果您可以将其发布在 jsfiddle 或某个编辑器中的某个位置,这将很有帮助,以便我们进一步了解它。
Thanks谢谢
Open package.json file, in directory of your App, then add this line (preferably under "private" line, as you can see in the picture below. This also works for any other url if your back-end is not located on your localhost.打开package.json文件,在您的应用程序目录中,然后添加此行(最好在“私有”行下,如下图所示。这也适用于任何其他 Z572D4E421E5E0B9BC11D815E 不位于您的后端本地主机。
"proxy": "http://localhost:3000/",
Remember to restart your server after this change!请记住在此更改后重新启动您的服务器!
For this you will need to allow CORS in your backend code for the URL you will be deploying, and you can use that URL as proxy.为此,您需要在您将要部署的 URL 的后端代码中允许 CORS,并且您可以使用该 URL 作为代理。 You can refer this documentation for detailed instructions.您可以参考此文档以获取详细说明。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.