Axios Interceptor Cancellation Request

Source: Internet
Author: User

Axios one of the most commonly used features, interceptors

axios.interceptors.response.use (          = = = Response = JSON     if return resp;     Throw New Error (resp.msg)  },   = =      {return  promise.reject (Error)    }  },)

After the data is returned in the background, the detection status code is 200, then the data is returned, otherwise, an exception is thrown

Another feature, cancel the request, the following from the official

 var  canceltoke = Axios. Canceltoken;  var  source = Canceltoken.source (); Axios.get (  '/user/12345 '  catch  (function   (thrown) { if   (Axiso.iscancel (thrown)) {Console.log ( ' Rquest canceled ')    Span style= "COLOR: #000000" >, thrown.message);  else  { // handle error  }});  //  Cancel the request (the information parameter set can be set)  source.cancel ("Operation is canceled by user"); 

Or CancelToken pass a executor function to the constructor to create a cancel token

var Canceltoken = Axios. Canceltoken; var cancel;axios.get ('/user/12345 ', {    new Canceltoken (function  Executor (c {        // This executor function accepts a cancel function as a parameter        , cancel = C;    })}); // Cancel the request cancel ();
In most cases, the methods we encapsulate are exported through the export,
So cancel we can also export output, so that you can call the export {cancel} at any time in the main business

Axios Interceptor Cancellation request

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.