添加授权标头时预检失败-React + Nodejs

时间:2018-12-07 10:03:11

标签: node.js reactjs axios

我正在尝试与nodejs服务器通信,添加授权标头时“ Preflight”失败,没有标头我就可以与服务器连接。

下添加了来自浏览器开发工具的请求标头的屏幕截图

CORS已添加到server.js

app.use(function(req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "Content-Type,Authorization");
    res.header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
    next();
});

反应JS-Axios代码

let params = {
      "userId": userName,
      "password": password,
      "random": event.target.name,
      "grant_type": "password"
    };
    var config = {
      headers: {
        'Authorization': 'Basic YXBwbGljYXRpb246c2VjcmV0',
        'Content-Type': 'application/x-www-form-urlencoded'
      }
    }


[Axios.post('/oauth/token', params, config).then((response) => {

      if (response.status === 200) {
        if (response.data.querySuccess) {
          console.log(response);
    }
    ).catch((error) => {
      console.log(error);
      alert.error('Oops some error occured please check the data you have submitted');
    });
  }][1]

0 个答案:

没有答案