XMLHttpRequest

For normal Ajax requests we abort the request with the xmlHttprequet.abort () method. When a request is terminated, its readyState is set to xmlHttprequet.unsent (0) and the status of the request is 0.

How can an HTTP request be cancelled

let xhr = new XMLHTTPRequest(), method = 'GET', url = "http:aaa.com" xhr.open( method,url,true ); xhr.send(); if(OH_NOES_WE_NEED_TO_CANCEL_RIGHT_NOW_OR_ELSE){ xhr.abort(); // Cancel the request}Copy the code

Axios cancel method

Axios is cancelled by canceltoken. source, as shown below

const CancelToken = axios.CancelToken();
const Source = CancelToken.source();
axios.get('http:aaa.com',{
    cancelToken: source.token
}).catch((thrown) => {
      if (axios.isCancel(thrown)) {
        console.log('Request canceled', thrown.message);
      } else {
        // handle error
      }
})

axios.post('http:aaa.com', {
  name: 'haha'
}, {
  cancelToken: source.token
})
source.cancel('Operation canceled by the user.');

Copy the code

How does axios canceltoken. source cancel the request?

In fact, AXIos wraps XMLHtpRequest, and the AXIos abort request is aborted using xmlHttprequest.abort ()