axios({
method: 'post',
url: 'http://localhost:8080/login',
data: {
username: obj.username,
password: obj.passwd
},
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: 3000,
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)
)