Axios Post,get Package, support data type

Source: Internet
Author: User

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

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.