vue-axios post请求content-type:application/x-www-form-urlencoded

axios的post请求,默认是application/json提交JSON格式的数据

实际我们后端要求的 'Content-Type': 'application/x-www-form-urlencoded' 

这时需要配置content-type:

axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';

这里使用了qs插件:

简单来说,qs 是一个增加了一些安全性的查询字符串解析和序列化字符串的库。

在项目中使用命令行工具输入:

npm install qs --save


安装完成后在需要用到的组件中:

import qs from "qs"

这里我是放在了main.js:

import qs from "qs"
Vue.prototype.$qs = qs;


具体使用中我查看了:qs.parse()和qs.stringify()

这两种方法虽然都是序列化,但是还是有区别的。
qs.parse()是将URL解析成对象的形式
qs.stringify()是将对象 序列化成URL的形式,以&进行拼接

demo:

vue-axios post请求content-type:application/x-www-form-urlencoded_第1张图片

vue-axios post请求content-type:application/x-www-form-urlencoded_第2张图片

你可能感兴趣的:(前端vue技术点干货分享,x-www-form,axios)