axios提交的表单转换成applicationx-www-form-urlencoded

it2023-10-20  69

当后台限定必须用application/x-www-form-urlencoded数据格式提交表单时

import Qs from "qs"; //需要转换的数据data method: "POST", headers: { "content-type": "application/x-www-form-urlencoded" }, data: Qs.stringify(data),

以下为axios操作的补充 拦截器的post请求可以添加Content-Type头部,若后台已经写死,不传也可以

const instance = axios.create({ baseURL: 'http://39.108.147.139:9091', timeout: 3000 }) instance.interceptors.request.use(function (config) { if(config.method == 'post'){ config.headers['Content-Type'] = 'application/x-www-form-urlencoded' } return config; }, function (error) { // 做一些与请求错误 return Promise.reject(error); });
最新回复(0)