直接上相关代码:
let data = {
"userId": userId,
};
let config = {
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
};
_this.$axios.post(url, _this.qs.stringify(data), config).then(function (res) {...
步骤:
1.正常定义数据
2.通过axios的config配置Content-Type为application/x-www-form-urlencoded
更多config配置说明详见:https://www.cnblogs.com/vickylinj/p/10889028.html
或官方文档:http://www.axios-js.com/docs/
3.qs.stringify序列化数据
附加说明:
*qs引入步骤:
1.安装:npm i qs
2.main.js引入:
import qs from 'qs'
3.全局声明:
// 引入qs传输数据序列化
Vue.prototype.qs=qs;
4.调用:_this.qs.stringify(data)
*qs使用application/x-www-form-urlencoded问题:
重点是:
post:
enctype
默认"application/x-www-form-urlencoded"
对表单数据进行编码,数据以键值对在http请求体重发送给服务器;
如果属性为
"multipart/form-data"
,则以消息的形式发送给服务器。
但是我们在vue里面直接提交的是一个对象 , 根本不是这个玩意
所以我们要借助node 里面的 qs
qs和JSON 差不多(但是qs.stringify和JSON.stringify结果不同,可console打印查看)