problem description
vue project mounts a div, click-and-renew request for axios request timeout interception, which encapsulates an axios,
that does not time out to display normally. After the timeout click, network has response data response, but the request on the vue page cannot receive res
related codes
/ / Please paste the code text below (do not replace the code with pictures)
introduce encapsulated axios into main.js
//
import request from "../static/js/request1.js";
Vue.prototype.axios = request
request1.js
import Axios from "axios"
import Qs from "qs"
import Vue from "vue"
Axios.defaults.timeout = 800
const request = (options) => new Promise((resolve, reject) => {
const page404 = (config) => {
document.getElementById("loadAgain").style.display="block";
let c = Vue.extend({
template:"<div id="loadAgain" @click="loadAgain"><div class="loadBox"><img src="static/img/woply_404@2x.png"/></div><p class="load-p"><p class="load-p"></div>",
methods: {
loadAgain() {
request(config)
document.getElementById("loadAgain").style.display="none";
}
}
})
new c().$mount("-sharploadAgain");
}
Axios.interceptors.response.use((response) => {
document.getElementById("loadAgain").style.display="none";
return response;
}, (error) => {
if(error.code=="ECONNABORTED"&& error.message.indexOf("timeout")!=-1){
//
let config = error.config;
page404(config);
}
return Promise.reject(error);
});
Axios.request(options)
.then((res) => {
if (typeof res.data == "string") {
res = JSON.parse(res)
}
if (res.data.code == "0000") {
resolve(res)
} else {
reject(res);
}
})
.catch((err) => {
reject(err);
})
})
export default {
get(URL,options) {
return request(Object.assign({}, options, { method: "get" ,url:URL}))
},
post(URL,params,options) {
return request(Object.assign({}, options, {
method: "post",
url:URL,
headers: {
"Content-Type": "application/x-www-form-urlencoded;charset=UTF-8",
},
data:params,
paramsSerializer: function(params) {
return Qs.stringify(params, {arrayFormat: "brackets"})
},
}))
},
}
result
thencatchresolve(res)