您的位置:首页 > Web前端 > Vue.js

解决vue2中使用axios http请求出现的问题

2018-03-05 15:02 771 查看

使用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);

大功告成!

问题解决!

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  vue http axios
相关文章推荐