axios封装post formData 表单传值

0 阅读1分钟
export function postFormData(url, data) {
  return service({
    url: url,
    method: 'post',
    headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
    transformRequest: [
      function (data) {
        let ret = '';
        for (const it in data) {
          const value = typeof data[it] === 'object' ? JSON.stringify(data[it]) : data[it];
          ret += encodeURIComponent(it) + '=' + encodeURIComponent(value) + '&';
        }
        return ret;
      },
    ],
    data,
  });
}