如何修复本地主机中的反应 cors 错误? [英] How to Fix react cors error in localhost?

查看:51
本文介绍了如何修复本地主机中的反应 cors 错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个 React 应用程序.我在哪里请求 AXIOS 的 API.但是当我运行 NPM START 在本地主机中测试我的应用程序时,我收到了 CORS 错误.这是错误从源 ' 在 'https://********.com/trx_status.php' 访问 XMLHttpRequesthttp://localhost:3000' 已被 CORS 策略阻止:Access-Control-Allow-Headers 在预检响应中不允许请求标头字段私钥.

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.

我是 React 新手.请告诉我如何解决这个问题.谢谢...

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

推荐答案

该错误是由发送到服务器的自定义 privatekey 标头引起的.此字段必须包含在来自服务器的 Access-Control-Allow-Headers 响应标头中.可以使用:

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

当使用 php 时,可以使用以下代码段:

when using php the following snippet can be used:

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

这篇关于如何修复本地主机中的反应 cors 错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆