我无法执行ReactJS的请求。标头无法正确写入

问题描述 投票:1回答:2
axios.get('http://localhost:8080/omp/patients', { headers: {authorization: 'Bearer ' + token}})
        .then( response => {
                this.state = response.data;
            }
        ).catch(ex=> {
            alert("You are not registered");
            //console.log(e)
        });

enter image description here

在网络中,我有一个字段:“访问控制请求标题:授权”,但没有字段“授权:令牌”

reactjs spring security spring-security axios
2个回答
0
投票

您将需要正确设置Authorization标头,如上面的注释。

关于您对CORS政策的答复:

您将需要了解CORS,但是,如果Chrome和其他浏览器在响应时未设置适当的CORS标头,则它们基本上会暂停网络请求。预检是发送到同一URL的初始请求,以在发送实际请求之前了解该URL是否支持CORS。服务器似乎不了解CORS的飞行前检查或配置不正确。

您有几个选择:

  1. 如果您有权访问服务器配置或代码(例如Laravel或NodeJS服务器),请安装CORS插件。
  2. 如果您是唯一使用此软件的用户,则可以为Chrome或Firefox或其他安装CORS浏览器扩展。

0
投票

将代码更改为此:

axios.get('http://localhost:8080/omp/patients', { headers: { "Authorization": 'Bearer ' + token}})
        .then( response => {
                this.state = response.data;
            }
        ).catch(ex=> {
            alert("You are not registered");
            //console.log(e)
        });

同样也在您运行服务器导入/请求cors模块的app.js / index.js中执行以下操作:

let cors = require('cors');
app.use(cors());

希望这会有所帮助!

© www.soinside.com 2019 - 2024. All rights reserved.