reactjs - 来自 CORS 预检 channel 的 CORS header ‘x-auth’ 中缺少 token ‘Access-Control-Allow-Headers’

标签 reactjs cors axios

我想使用post方法从axios包和xampp服务器获取数据, 我在使用 Firefox 时遇到错误:

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://127.0.0.1/aftab/inventory3/v1/repository/all. (Reason: missing token ‘x-auth’ in CORS header ‘Access-Control-Allow-Headers’ from CORS preflight channel).[Learn More] Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://127.0.0.1/aftab/inventory3/v1/repository/all. (Reason: CORS request did not succeed)

但是当在 Chrome 中测试它时,它工作正常并且没有任何错误,

我的axios请求代码是:

let page = 1;
    let config =
        {
            'Content-Type': 'application/x-www-form-urlencoded',
            'x-auth': localStorage.getItem("token"),
        };
    let data = {page: page};
    if (localStorage.getItem("token")) {
        await axios({
            method: "post",
            url: "http://127.0.0.1/aftab/inventory3/v1/repository/all",
            data: data,
            headers: config
        }).then(function (response) {
            console.log(response);
        }).catch(function (error) {
            if (error.response) {
                cosole.log(response);
            } else if (error.request) {

               console.log(error.request);
            } else {

                console.log('Error', error.message);
            }
            console.log(error.config);
        });
    } else {
        localStorage.removeItem("token");
        // this.history.push('/log/in');
    }

在 Firefox 控制台中:

firefox console

在 Firefox 控制台网络中,仅发送了选项方法,之后我们没有任何请求,但在 chrome 中它工作正常,并且在选项方法之后我们有一个 post 方法请求

最佳答案

将您的配置更改为如下所示

let config =
        {
            'Content-Type': 'application/x-www-form-urlencoded',
            'x-auth': localStorage.getItem("token"),
            'Access-Control-Allow-Origin': '*'
        };

并在您的服务器上启用 cors

关于reactjs - 来自 CORS 预检 channel 的 CORS header ‘x-auth’ 中缺少 token ‘Access-Control-Allow-Headers’,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54663593/

相关文章:

javascript - 从旧的外部 Javascript 更改 React 组件的状态?

laravel cors "Class cors does not exist"错误

php - 禁止跨域飞行前选项

reactjs - React Redux Axios : POST Request not receiving credentials from redux state

javascript - VueJS 数组索引返回错误结果

javascript - 未处理的拒绝 (TypeError) : respo. json 不是函数

javascript - 我试图在谁赢谁输的答案弹出之前延迟(剪刀石头布游戏)

node.js - React中第一次没有从nodejs服务器获取数据

reactjs - React 无法识别 DOM 元素上的 `computedMatch` 属性。

ajax - 引入预检 CORS 请求的动机是什么?