AXIOS uses interceptors to uniformly process all HTTP requests

AXIOS uses interceptors

intercepted them before the request or response is treated by THEN or CATCH.

// Add request interceptor AXIOS.ISTERCEPTORS.REQUEST.USE (Function) { / / Do what Return Config;}, function (error) {// before sending a request,};};
   

// Add Response Interceptor AXIOS.ISTERCEPTORS.RESPONSE.USE (Function (response) {// Response data Do something RETURN RESPONSE;}, function (error) {// Response error Do something Return Promise.reject (Error);});

 Remove interceptor   
var myinterceptor = axios.interceptors.Request.Use (Function () {/ *…*/} ;axios

   

  To customize the AXIOS instance  
Var instance = apios.create (); instance.interceptors.Request.Use (Function () {/*…*/} );


PS: Next, the AXIOS interceptor is used

  Import Axios from 'Axios' / / AXIOS reference import store from '../../store' // store reference const serivce = axios.create ({// creation service BaseURL: 'http://test.api.rujiaowang.net', // foundation Path Timeout: 5000 // Request Delay}) serivce.interceptors.Request.Use (// Request interception config => {if (store.getters.usertoken) {config.headers ['x-token'] = store.getters .userToken config.headers [ 'user-Type'] = store.getters.userType store.getters.userType:? '' // store user information in the request header config.onUploadProgress = (progressEvent) => {var complete = (progressEvent .loaded / progressEvent.total * 100 | 0) + '%' store.dispatch ('setuplOADPERCENT ', Complete)}} Return config}, error => {return promise.reject (error)}) serivce.interceptors.response.Use (// Reply interception, mainly for partial return data status code for processing Response => {RETURN Response}, Error => {Return Promise.reject (Error)} Export Default Server  
Summary

The AXIOS that Xiaobian introduced to you use the interceptor to unify all HTTP request methods, I hope that everyone can help, if you have any questions, please leave a message, the small package will reply to everyone. Thank you very much for your support of Tumi Cloud Website!

© Copyright Notice
THE END
Just support it if you like
like0
share
comment Grab the couch

Please log in to comment