我正在尝试使用axios从react js登录到ASP.NET身份,但服务器给出了错误的请求。客户端代码如下所示。
var data = this.state;
data = JSON.stringify(data);
const response = axios({
url: "http://localhost:56885/Token",
method: "post",
headers: {
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"
},
data: data
});
但同样的请求在Postman中也是有效的:
发布于 2019-03-24 17:10:19
我犯了个小错误。内容类型是url编码的,但我发送的是json数据。感谢@ADyson,他识别了isue。
以下是正确的代码。
var data = this.state;
var bodyData =
"username=" +
data.username +
"&password=" +
data.password +
"&grant_type=password";
const response = axios({
url: "http://localhost:56885/Token",
method: "post",
headers: {
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"
},
data: bodyData
});
console.log(response);
https://stackoverflow.com/questions/55321237
复制相似问题