Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

vue axios设置headers中Content-Type无效如果解决?

我在一个请求中需要设置单独的Content-Type,于是在拦截器中这么处理

service.interceptors.request.use(config => {
  console.log(config)
  // 修改请求头信息
  if (config.type == 'change') {
    config.headers['Content-Type'] = 'application/x-www-form-urlencoded'
  } else {
    config.headers['Content-Type'] = 'application/json'
  }
  if (localStorage.getItem('token')) {
    config.headers['Authorization'] = 'Bearer ' + localStorage.getItem('token')
  }
  // config.headers["Authorization"] =
  //   "Bearer eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiIxIiwiZXhwIjoxNTUwODI5OTgyLCJpYXQiOjE1NTAyMjUxODJ9.fYZl_ShWh4ht-yWXdDKJZQVh0yyJL8-i2pMGS7cPodaezAQW6Y3wCmvJnxOTmc2E9Yd1apyxxIXPgnsJFIkbXg";
  return config
})

这是定义的api

export function editNoticeStatus (params) {
  return request({
    url: baserUrl + 'api/alarmInform/updateStatus',
    method: 'put',
    type: 'change',
    data: params
  })
}

调用的方法

  const params = {
    id: this.$route.query.id,
    status: '1'
   }
 axios.editNoticeStatus(params).then(res => {
    if (res.data.success) {
       this.$notify.success({
        title: '提示',
        message: res.data.message
       })
      } 
    })

但是打印出来是显示成功的
image.png
在network中却没有改变
image.png
请问这种情况应该如何解决?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

已解决,qs序列化一下可以了
import qs from 'qs'

// 修改通知状态
export function editNoticeStatus (params) {
  return request({
    url: baserUrl + 'api/alarmInform/updateStatus',
    method: 'put',
    type: 'change',
    data: qs.stringify(params) 
  })
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...