Import Axios from ' Axios 'Import Store from‘.. /vuex/store 'Import Router from‘.. /router/index 'Import {stringify} from' Qs '//qs Module//Axios ConfigurationAxios.defaults.timeout = 5000; Axios.defaults.baseURL= ";//' http://hzzly.net:4000 ';//http request Blockeraxios.interceptors.request.use (config= { if(store.state.token) {config.headers.Authorization=' token ${store.state.token} '; } returnconfig; }, Err= { returnPromise.reject (ERR); });//HTTP Response InterceptorAxios.interceptors.response.use (Response= { returnresponse; }, Error= { if(error.response) {Switch(error.response.status) { Case401: //401 Remove token information and jump to login page //Store.commit (types. LOGOUT);router.replace ({path:' Login ', query: {Redirect:router.currentRoute.fullPath}}); Break; Case403: Break; Case504: Break; Case500: //Store.dispatch (types. AJAX_ERROR,500); Break; Case404: //Store.dispatch (types. ajax_error,404); Break; }} console.log (Json.stringify (Error));//Console:Error:Request failed with status code 402 returnpromise.reject (Error.response.data)});/*{type:type, params:params,}*///Encapsulating RequestsExportfunctionfetch (URL, options) {varopt = Options | | {} return NewPromise (Resolve, reject) ={Axios ({method:opt.type|| ' Post ', Url:url, Params:opt.params|| {}, //determines if there is a custom header to serialize the parameters. Does not define a header, and the parameter is serialized to the query string by default. Data: (Opt.headers opt.data:stringify (opt.data)) | |{}, ResponseType:opt.dataType|| ' JSON ', //Set the default request headerHeaders:opt.headers | | {' Content-type ': ' application/x-www-form-urlencoded; Charset=utf-8 '}}). Then (response= { if(Response.data.code = = 0) {Resolve (Response.data)}Else if(Response.data.code = = = ' 000 ')) {Resolve (Response.data)}Else{reject (Response.data)//store.commit (' set_loading ', false) } }).Catch(Error ={console.log (Error) reject (error)//store.commit (' set_loading ', false)})})}exportdefaultAxios
Features using the Axios package
Import {fetch} from '. /.. /util/api '// User Login Export const userlogin= (data) = Fetch ('/api/users/api/userlogin ', { Data, ' type ': ' Post '});
Axios Post,get Package, support data type