Encapsulating Axios Requests

Source: Internet
Author: User

Import Axios from ' Axios ' import {Message} from ' Element-ui ' import store from ' @/store/store ' import qs from ' qs ' import { GetToken} from ' @/utils/auth ' import {commonparams} from ' @/api/config ' import router from ' @/router/index.js ' let Cancel, Promisearr = {}const Canceltoken = Axios. canceltoken;//Create Axios default Request Const SERVICE = Axios.create ({baseURL: "http://localhost:8000/bpm/", timeout:5000,//RE         Quest Timeout,})//request to intercept service.interceptors.request.use (config = {//To prevent repeated requests for Ajax if (Promisearr[config.url]) { Promisearr[config.url] (' action Cancel ') Promisearr[config.url] = cancel} else {Promisearr[config.url] = Cancel}//splicing common parameter Config.params = object.assign ({},commonparams,config.params)//When the parameter is serialized for the traditional receive mode post I            F (Config.method = = = "Put" | | Config.method = = = "Delete") {//serialization config.headers = {' Content-type ': "Appl Ication/x-www-form-urlencoded;charset=utf-8 "//When Post isWhen the request header needs to be serialized};        Config.data = Qs.stringify (Config.data); }//whether to carry a cookie without knowing if (Store.getters.token) {config.headers[' token '] = GetToken ()//Let each request carry token--[' token '] To customize the key, please modify it according to the actual situation} return config}, error = {//do something with request error Console.log (Error)//F        or debug Promise.reject (Error)})//corresponding intercept service.interceptors.response.use (response = response, error = { if (error) {switch (error.response.status) {case 401://Router.push                    ({//path: "/login"//});                Error.response.statusText = ' Unauthorized, please re-login ' break;                        Case 404:error.response.statustext = ' request error, the resource not found ' Message ({               Are you hungry? Popup widget, similar to toast Showclose:true, Message:error.response.statusText,          Type: "Error", center:true});            Break }} else {error.response.statusText = "connection to server failed"} return Promise.reject (Error)})                Export default {//get request get (Url,param) {return new Promise ((resolve,reject) = {Service ({ Method: ' Get ', url, Params:param,//Cancel Request function Create C                Anceltoken:new Canceltoken (c = = {Cancel = c})}). Then (res = { Resolve (RES)}). catch (error=>{reject (Error)})}),//pos                T request post (Url,param) {return new Promise ((resolve,reject) = {service.post (URL, param,//Cancel Request function creation {Canceltoken:new Canceltoken (c =                        {Cancel = c}). Then (RES-= {Resolve (res)}). catch (error=>{Reject (Error)})})}}

Request with serialization

Encapsulating Axios Requests

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.