What is the best way to deal with token expiration after the front and rear ends are separated?

our project used to deal with token expiration. The back end returns an expired error code and the front end jumps to the login page.
A page is a form page. After successful submission, jump to B page

function request(url,data,header) {
 $.ajax({
  url: url,
  type: "post",
  data: data,
  header: header,//headertoken
  success: function(data) {
    //code1
    if(data.code == 1) {
       window.location.href = "B.html"
    }
    //code2 token
    if(data.code == 2) {
       window.location.href = "login.html"
    }
  }
})
}

request("submitUrl",data,header)//

customers feel that this interactive experience is too poor, so they want to change it to
when the token expires, the backend returns an expired error code, frontend to judge and then sends a request to update the token before sending the request, so that the user can complete the process without doing anything.

function request(url,data,header) {
 $.ajax({
  url: url,
  type: "post",
  data: data,
  header: header,//headertoken
  success: function(data) {
    //code1
    if(data.code == 1) {
       window.location.href = "B.html"
    }
    //code2 token
    if(data.code == 2) {
       window.header.token = 
       requestToken(header)//token
    }
  }
})
}

request("submitUrl",data)//

//token
function requestToken() {
$.ajax({
  url: "getToken",
  data: data,
  type:"post",
  success: function(data) {
    //code1
    if(data.code == 1) {
       return data.token
    }
    
  }
})

but I was stuck by a process problem. How can I send the previous request again (without letting the user operate) after getting this updated token,? is it feasible for me to think so?

Jan.23,2022

< H1 > interceptor. < / H1 >

Global intercept determines whether code needs to be refreshed when sending a request.
if yes, save the session, refresh the token and then resend it.
or send the currently requested Request together when refreshing the token interface. The backend processes directly, both refreshing the token and processing the business and returning the business results. But in this way, the back end usually won't do it. @ @


change it to token that will never fail


execute request
such as

after the requestToken executes successfully.
  

process:

  1. send request, record request information
  2. token expires, return 40x
  3. re-obtain token
    3.1 successful, resend the record request.
    3.2 failed, jump login, to log in again.
Menu