1. 程式人生 > >vue2中使用axios http請求出現的問題解決

vue2中使用axios http請求出現的問題解決

使用axios處理post請求時,出現的問題解決

預設情況下: axios.post(url, params).then(res => res.data);

當url是遠端介面連結時,會報404的錯誤:
Uncaught (in promise) Error: Request failed with status code 404

我們需要例項化一個新的axios,並且設定他的訊息頭為’content-type’: ‘application/x-www-form-urlencoded’

於是得出解決方案:

var instance = axios.create({
    headers: {'content-type
': 'application/x-www-form-urlencoded'}
}); instance .post(`url`, params).then(res => res.data);

然後發現不報錯了,但是後臺接受不到傳入引數,查閱資料,發現需要引入一個qs模組

var qs=require('qs');
var instance = axios.create({
    headers: {'content-type': 'application/x-www-form-urlencoded'}
});
instance .post(`url`, qs.stringify(params)).then
(res => res.data);

大功告成
問題解決