当前位置:   article > 正文

关于如何在vue中使用axios时候通过formData发送数据_vue3 ant design vue axios 传formdata数据

vue3 ant design vue axios 传formdata数据

参考https://www.jianshu.com/p/b10454ed38ba

之前写axios请求的时候,发现后端只能通过URL后面绑定参数的形式传值,无法无法识别到前端穿过来的对象值,
但是我传值都很标准,不可能出现问题
在这里插入图片描述

在这里插入图片描述

然后我通过postman测试,发现后端需要用formData才能看到后端传过来的对象值
在这里插入图片描述

然后现在问题发现了,原来是因为axios传到后台的不是formdata数据,现在解决的方法也很简单,加上如上的内容就可以了,具体什么原因以后再细细研究。

这一段加在main.js上,然后修改完毕后再使用哦.

axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
axios.defaults.headers.get['Content-Type'] = 'application/x-www-form-urlencoded';
axios.defaults.transformRequest = [function (data) {
    let ret = ''
    for (let it in data) {
      ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
    }
    return ret
}]
Vue.prototype.$axios = axios;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

成了
在这里插入图片描述

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/木道寻08/article/detail/758587
推荐阅读
相关标签
  

闽ICP备14008679号