这篇文章主要为大家详细介绍了解决vue2中使用axios http请求出现的问题,具有一定的参考价值,可以用来参考一下。
感兴趣的小伙伴,下面一起跟随四海网的小编两巴掌来看看吧!
使用axios处理post请求时,出现的问题解决
当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);
大功告成!
问题解决!
以上这篇解决vue2中使用axios http请求出现的问题就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持四海网。
本文来自:http://www.q1010.com/184/4449-0.html
注:关于解决vue2中使用axios http请求出现的问题的内容就先介绍到这里,更多相关文章的可以留意四海网的其他信息。
关键词:vue.js
四海网收集整理一些常用的php代码,JS代码,数据库mysql等技术文章。