Payload Content-Type: 'application/json; charset=utf-8'
Form Data Content-Type: 'application/x-www-form-urlencoded'
axios({
method: 'post',
url: url,
data: {
username: username,
password: password
},
transformRequest: [
function (data) {
let ret = ''
for (let it in data) {
ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
}
ret = ret.substring(0, ret.lastIndexOf('&'));
return ret
}
],
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
})
全局设置
import axios from 'axios'
import qs from 'qs'
// 实例对象
let instance = axios.create({
timeout: 15000,
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
})
// 请求拦截器
instance.interceptors.request.use(
config => {
config.data = qs.stringify(config.data) // 转为formdata数据格式
return config
},
error => Promise.error(error)
)
网友评论