import Vue from 'vue' import qs from 'qs' import axios from 'axios' axios.defaults.baseURL = 'http://192.168.2.75:9501/internal/'; axios.defaults.headers['Content-Type'] = 'application/x-www-form-urlencoded'; /**http request 拦截器**/ axios.interceptors.request.use( req => { let method = (req.method).toLocaleLowerCase();//获取请求的方式(get,post,delete,options) req.data = qs.stringify(req.data);//传入的值必须进行转换 if (method == "get") { } else { req.headers = { 'Content-Type': 'application/x-www-form-urlencoded;'//解决请求跨域 } } return req; }, err => { return Promise.reject(err); } ); /**http response 拦截器 非必要**/ axios.interceptors.response.use( res => { // console.log(res); if (res.status != 200) { //做一些错误处理,如跳转到登录页等 } return res.data; }, err => { return Promise.reject(err.response); } ); Vue.prototype.$post = function (url = "", data = {}) { return axios.post(url, data); }; Vue.prototype.$get = function (url = "", data = {}) { // data = qs.stringify(data); return axios.get(url, data); }; Vue.prototype.$remove = function (url = "", data = {}) { return axios({ url, method: 'delete', data: data }) };