[英]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.