Axios里POST application/x-www-form-urlencoded data格式不对

axios发起post请求,后台需要form表单形式,按照文档发送post请求,但是发送数据变成字符串形式,后面还多了个分号。

this.$axios
      .post(Api.initCheapOrder, {name: 'zs'}, {'headers': {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'}})  
        .then(res => {
              .......
        }).catch(e=>{......})
image.png

解决办法,引入'qs'模块,把数据转换一下

import Qs from 'qs'
this.$axios
      .post(Api.initCheapOrder, Qs.stringify({name: 'zs'}), {'headers': {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'}})  
        .then(res => {
              .......
        }).catch(e=>{......})
image.png

你可能感兴趣的:(Axios里POST application/x-www-form-urlencoded data格式不对)