默认请求数据的格式是这样的:
修改请求头信息
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
修改请求数据
// 修改请求数据
transformRequest: [function (data, headers) {
let ret = ''
for (let it in data) {
// 去除空字符串的请求字段
if (data[it] !== '') {
if (ret !== '') ret += '&'
ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it])
}
}
return ret
}],
关于axios的具体代码配置如下:
const initFetch = (baseUrl, router) => {
const instance = axios.create({
// 设置超时时间 -30秒
timeout: 30000,
// 请求的baseUrl
baseURL: baseUrl,
// 请求头部信息
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
// 修改请求数据
transformRequest: [function (data, headers) {
let ret = ''
for (let it in data) {
// 去除空字符串的请求字段
if (data[it] !== '') {
if (ret !== '') ret += '&'
ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it])
}
}
return ret
}],
// 跨域请求时允许携带凭证(cookie)
withCredentials: true
})