What is the reason why the axios timeout mount click event re-request did not enter the foreground then and catch,?

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)

Apr.25,2021

the problem lies here

if (typeof res.data == 'string') {
        res = JSON.parse(res)
      }
Change

to

if (typeof res == 'string') {
        res = JSON.parse(res)
      }

clipboard.png

if you take a closer look at your code, it has nothing to do with resolve. The key is still here
you determine whether res.data is string, but what you parse is that res, will report an error when parsing an object
and then this error will not be directly caused by catch to request you to enter reject

.
if (typeof res == 'string') {
    res = JSON.parse(res)
 }

problem solved. The get and post methods are changed to return a promise,page404. Resolve is also needed in it

.
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).then((res) => { if (typeof res == 'string') { res = JSON.parse(res) } resolve(res); }) .catch((err) => { }) document.getElementById('loadAgain').style.display='none'; } } }) new c().$mount('-sharploadAgain'); }
export default {
    get: (URL,options) => new Promise((resolve, reject) => {
        return request(Object.assign({}, options, { method: "get" ,url:URL})).then(res => {resolve(res)})
    }),
    post: (URL,params,options) => new Promise((resolve, reject) => {
        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'})
          },
        })).then(res => {resolve(res)})
    })
}
Menu