axios使用post方法,后台接收不到数据

  1. 基本post请求后,可以看到是以Request Payload的形式传送了参数,不是form-data形式。此时添加头部headers。
this.axios.post('url',{
    userCardId: promptValue
}).then(res => {

 })

在这里插入图片描述 在这里插入图片描述
2. 添加headers:{‘Content-Type’:‘application/x-www-form-urlencoded’}; 后,结果如下,Form Data中传递了一个对象。

this.axios({
    method:'post',
    url:'url',
    data:{userCardId:promptValue},
    headers:{'Content-Type':'application/x-www-form-urlencoded'},
}).then(res =>{
                      
}) 

在这里插入图片描述 在这里插入图片描述
3. 继续添加transformRequest部分,做数据转换。此时后端能够接收到数据了。

this.axios({
    method:'post',
    url:'url',
    data:{userCardId:promptValue},
    headers:{'Content-Type':'application/x-www-form-urlencoded'},
    transformRequest:[function (data) {
        let ret = ''
        for (let it in data) {
            ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
         }
            return ret
     }],
 }).then(res =>{
                    
 }) 

在这里插入图片描述 在这里插入图片描述
https://github.com/Gesj-yean/vue-demo-collection 记录了更多优秀插件的使用方法。有时间的同学请看我的置顶博客,可太感谢啦。

发布了27 篇原创文章 · 获赞 4 · 访问量 2832

猜你喜欢

转载自blog.csdn.net/qq_39083496/article/details/89382774