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 大写

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐