application/x-www-form-urlencoded 请求

重构老项目,发现后端接口支持的请求类型是 form-data,导致 axios 请求失败。
找到了一些解决方案,发现这种方式侵入性小,比较靠谱,所以记录一下。
axios 发送 form-data 或者 x-www-form-urlencoded 请求的配置 ↓↓↓

import request from '@/utils/request'

// 查询心率
export function listHeartRate(data) {
  return request({
    url: 'api/heartHealth/queryHeartRateList',
    method: 'post',
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: data,
    transformRequest: [
      function (data) {
        let ret = ''
        for (let it in data) {
            ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
        }
        ret = ret.substring(0, ret.lastIndexOf('&'));
        return ret
      }
    ],
  })
}
posted @ 2022-02-14 17:46  Better-HTQ  阅读(499)  评论(0编辑  收藏  举报