[英]passing header in axios error: 400 bad request
I am trying authorization to my react app using axios and that request works with postman but when I start react it gives me 400 bad request error我正在尝试使用 axios 对我的反应应用程序进行授权,并且该请求适用于 postman 但是当我开始反应时它给了我 400 错误请求错误
let user = JSON.parse(localStorage.getItem("user"));
let userName = user.userName;
let accessToken = user.accessToken;
console.log(accessToken);
let config = {
headers: {
'Authorization': 'Bearer ' + accessToken
}
}
console.log(config);
axios.post("http://localhost:8080/getUserType",config).then((response) => {
var res=response.data;
this.setState({loginValue:res.userType});
console.log(res.userType);
}).catch((err) => {
console.log(err);
})
}
console return this控制台返回这个
Dashboard.js:32 Error: Request failed with status code 400
at createError (createError.js:16)
at settle (settle.js:17)
at XMLHttpRequest.handleLoad (xhr.js:61)
Maybe try to directly use the headers
instead of encapsulating them in a config variable.也许尝试直接使用headers
而不是将它们封装在配置变量中。
let user = JSON.parse(localStorage.getItem("user"));
let userName = user.userName;
let accessToken = user.accessToken;
console.log(accessToken);
let headers: {
'Authorization': 'Bearer ' + accessToken
};
console.log(config);
axios.post("http://localhost:8080/getUserType", { headers }).then((response) => {
var res=response.data;
this.setState({loginValue:res.userType});
console.log(res.userType);
}).catch((err) => {
console.log(err);
})
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.