vue中配置请求参数FormData格式

有时候需要给后端传递的参数是FormData(表单)格式(默认是json格式)

// 在main.js中配置如下代码
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded'
 axios.defaults.transformRequest = [function (data) {
    
    
  let ret = ''
  for (const it in data) {
    
    
    ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
   }
  return ret
 }]

猜你喜欢

转载自blog.csdn.net/A_Brave/article/details/110789026
今日推荐