拦截器的使用
axios .interceptors.response.use(response => {
// 与后台约定的返回码
if (response.data.retCode == 3001) {
window.location.href = response.data.redirectUrl;
}
return response; // 响应参数
}, (responseError) => {
console.log("responseError", responseError);
})
复制代码
axios.get()
axios.get(url, {params: query}).then(response => {
....// business logical
})
复制代码
axios.post()
axios.post(url, {params: query}).then(response => {
.../ business logical
})
复制代码
axios
// get
axios({
url: `${interfaceURL}`,
method: `get`,
params: {
query1: ...
},
headers: {
`Content-Type`:`application/x-www-form-urlencoded`
}
}).then(res => {
// business logical
})
复制代码
let query = {...};
//post
axios({
url: `${interfaceURL}`,
method: `post`,
data: query,
transformRequest: [
function(data) {
return JSON.strigify(data)
}
],
headers: {
`Content-Type`: `application/json; charset=utf-8`
}
}).then(res => {
// business logical
})
复制代码