ajax.js

'use strict' import axios from 'axios' import { Notification } from 'element-ui' const ajax = axios.create({ timeout: 8000, // header: { // 'Content-Type': 'application/json', // 'X-Requested-With': 'XMLHttpRequest' // }, responseType: 'json}) / / the unified handling of requests into the ajax. The interceptors. Request. Use (function (config) {config. Data = {des:' data - mark - server, SRC: 'data-mark-front', params: Config. Data} / / request header to add token const token = window. The localStorage. The getItem (' token ') if (token) {config. Headers. Authorization = token } return config }, Function (error) {return Promise. Reject (error)}) / / of the unified handling ajax. The interceptors. Response. Use (function (res) {const { Data, config} = res if(! data || ! Data.code) return Notification({title: 'no message returned ', type: 'error'}) // Token invalid, If (data.code == '-1') return logout() // No permission to call the interface if(data.code == '-2') return Notification({title: 'Insufficient permission, no interface call permission ', type: If (config.url.indexof ('/url/special') > -1) {if(data) return data.content} return data}, Function (error) {Notification({title: 'error '+ error, type: 'error' }) return Promise.reject((error)) } ) const logout = () => { window.localStorage.clear() window.sessionStorage.clear() window.location.replace('/login') }Copy the code

Reference API. Js

import ajax from '.. /ajax.js' const baseUrl = 'data/base' // get request export const getEnv = () => ajax.get(baseUrl+ '/env') // post request export const getList = params => ajax.post(baseUrl+ '/getList', Export const getDetail = params => ajax.post(baseUrl+ '/getDetail ', params) .then(res => res.resultData)Copy the code

Write at the end:

1. After using transformResponse, use json.parse (res) to convert to an object

2. The interface of the returned data execution sequence: 1. 2 transformResponse interceptors. Response. Use