'Content-Type': 'application/x-www-form-urlencoded'格式请求参数是后端无法解析

482 阅读1分钟

使用'Content-Type': 'application/x-www-form-urlencoded'格式请求参数是后端无法解析对应的参数的时候 可以采用一下的解决方案
方案一

const qs =  require('qs)
export function getDiagramPost(params){
  return request({
     url: '/xxx/xxx/xxx',
    method: 'post',
    data: qs.stringify(params),
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
  })
}

方案二

export function getDiagramPost(params){
  const qs = new URLSearchParams()
  Object.keys(params).forEach(val=>{
    qs.append(val,params[val])
  })
  return request({
    url: '/xxx/xxx/xxx',
    method: 'post',
    data: qs,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
  })
}