[英]cannot post in axios in vue.js
我试过这个代码
axios
.post("http://localhost:3010/user/login", {
headers: {
"Content-type": "application/json"
},
body: JSON.stringify({ username: "username", password: "password" })
})
.then(response => {
this.resp = response;
})
.catch(e => {
console.error(e);
});
但响应它无效登录但它在邮递员中工作
它有什么问题?
在这样的网络响应中
当您使用 post 发送对象时,它会被转换为字符串,因此您有效地发送到 API 端点的是:
JSON.stringify(JSON.stringify({ username: "username", password: "password" }))
没有必要
此外,您不会将正文作为标题的一部分发送。
https://github.com/axios/axios#request-method-aliases
axios.post(url[, data[, config]])
在您的情况下,这意味着您发送三个参数,url,然后是数据,然后是选项。 由于您发送的唯一标头是 json 数据,并且 axios 可以为您处理,因此不需要这种情况下的选项,因此您可以只使用前两个参数
axios
.post(
"http://localhost:3010/user/login",
{
username: "username",
password: "password"
}
)
.then(response => {
this.resp = response;
})
.catch(e => {
console.error(e);
});
更改标题来自:
headers: {
"Content-type": "application/json"
}
到
headers: {
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
}
现在尝试,它对我有用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.