vue 数据请求content-type 全局设置和部分覆盖设置

1.全局设置:
在request.js中设置

axios.defaults.headers['Content-Type'] = 'application/json;charset=utf-8'

2.部分接口不是json 格式传输时,设置单独的 Content-Type

headers: {
 'Content-Type': 'application/x-www-form-urlencoded'
 },
// 登录方法
export function login(username, password, code, uuid) {
  const data = {
    username:username,
    password:password
  }
  return request({
    url: '/user/login',
    method: 'post',
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: data,
    transformRequest: [function (data) {
      // 数据默认会以json格式传递,需要转成key-value
      let ret = '';
      for (let i in data) {
        ret += encodeURIComponent(i) + '=' + encodeURIComponent(data[i]) + '&'
      }
      return ret.slice(0, -1);
    }],
  })
}

注意: Content-Type 大写

更多推荐

vue 数据请求content-type 全局设置和部分覆盖设置