当前位置:Gxlcms > JavaScript > vue2中使用axioshttp请求出现的问题与解决办法

vue2中使用axioshttp请求出现的问题与解决办法

时间:2021-07-01 10:21:17 帮助过:32人阅读

本文主要为大家分享一篇解决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);

大功告成!

问题解决!

相关推荐:

node.js通过axios如何实现网络请求

VueJs搭建Axios接口请求工具实例详解

关于vue2.0设置proxyTable使用axios进行跨域请求

以上就是vue2中使用axios http请求出现的问题与解决办法的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行