axios 自定义 content-type 没有生效

使用 post 提交表单数据
自定义修改了

'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8' 

为什么看到的请求还是Content-Type: text/plain;charset=UTF-8

clipboard.png

下面是封装的请求

function apiAxios (method, url, params) {
  return new Promise((resolve, reject) => {
    axios({
      method: method,
      url: url,
      data: method === 'POST' || method === 'PUT' ? params : null,
      params: method === 'GET' || method === 'DELETE' ? params : null,
      headers: method === 'POST' || method === 'PUT' ? { 'Content-Type': 'application/json;charset=UTF-8' } : { 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8' }
    }).then(function (res) {
      resolve(res)
    }).then(function (err) {
      reject(err)
    })
  })
}

export default {
  get: function (url, params) {
    return apiAxios('GET', url, params)
  },
  post: function (url, data) {
    return apiAxios('POST', url, data)
  },
  put: function (url, data) {
    return apiAxios('PUT', url, data)
  },
  delete: function (url, params) {
    return apiAxios('DELETE', url, params)
  }
}
阅读 3.8k
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进