这次给大家带来为什么vue2中不能使用axios http请求,解决vue2中不能使用axios http的注意事项有哪些,下面就是实战案例,一起来看一下。
使用axios处理注意事项时,出现的问题解决
默认情况下: 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);
登录后复制
大功告成!
问题解决!
相信看了本文案例你已经掌握了方法,更多精彩请关注【创想鸟】其它相关文章!
推荐阅读:
注意事项
注意事项
以上就是为什么vue2中不能使用axios http请求的详细内容,更多请关注【创想鸟】其它相关文章!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。