简体   繁体   English

如何修复 localhost 中的反应 cors 错误?

[英]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.

相关问题 Ajax的本地主机CORS错误 - ajax localhost cors error 即使在后端设置了 cors,如何修复 cors 错误 - How to fix cors error faced even with cors setup on the backend 从反应应用程序向快递服务器发送请求时如何修复CORS错误 - How to fix CORS error when sending a request to express server from react app 如何仅在Safari中修复XMLHttpRequest cors错误? - How to fix XMLHttpRequest cors error in Safari only? 如何使用凭证修复CORS错误:包含? - How to fix CORS error with credentials: include? React js Cors 策略问题 axios api 在 localhost 如何解决? - React js Cors policy Issue with axios api In localhost How to resove it? 如何将404错误XMLHttpRequest修复为本地主机 - How to fix 404 error XMLHttpRequest to a localhost 如何解决 React 和 Node/Express 中的 CORS 错误 - How to solve CORS error in React and Node/Express 尝试从 localhost:8080 向 localhost:3000 发出 GET 请求,但我收到 cors 错误,并在我的 node.js 服务器上安装 cors 无法修复它 - Trying to make a GET request from localhost:8080 to localhost:3000 but I get a cors error, and installing cors on my node.js server doesn't fix it 如何修复 Firebase 400 连接错误和 CORS 错误? - How to fix Firebase 400 Connection Error AND CORS error?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM