You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
49 lines
1.2 KiB
49 lines
1.2 KiB
import axios from "axios"; |
|
|
|
//默认超时时间 |
|
// axios.defaults.timeout = 1000 * 60 *; |
|
//跨域请求,允许保存cookie |
|
axios.defaults.withCredentials = true; |
|
|
|
//返回其他状态码 |
|
axios.defaults.validateStatus = function (status) { |
|
return status >= 200 && status <= 500; |
|
}; |
|
|
|
//HTTP Request拦截 |
|
axios.interceptors.request.use( |
|
(config) => { |
|
if(config.headers["Content-Type"] != undefined){ |
|
config.headers["Content-Type"] = "application/x-www-form-urlencoded"; |
|
} |
|
else{ |
|
config.headers["Content-Type"] = "application/json"; |
|
} |
|
return config; |
|
}, |
|
(error) => { |
|
console.error("Request interceptors:", error); |
|
return Promise.reject(error); |
|
} |
|
); |
|
|
|
//HTTP Response拦截 |
|
axios.interceptors.response.use( |
|
(res) => { |
|
const ok = res.data.Success || res.data.status == '200' || false, |
|
status = res.status || 200, |
|
message = res.data.AllMessages || res.data.errorMessage || res.data.message || "Internal Server Error!"; |
|
|
|
if (!ok && !status) { |
|
return Promise.reject(new Error(message)); |
|
} |
|
|
|
return res.data; |
|
}, |
|
(error) => { |
|
console.error("Response interceptors:", error); |
|
return Promise.reject(error); |
|
} |
|
); |
|
|
|
export default axios;
|
|
|