一、、配置config文件中的代理地址
这是我配置的地址:https://editor.csdn.net/md/?articleId=108807664
二、、配置axios , 创建axios.js
// 导入axios
import axios from 'axios'
// 使用element-plus Message做消息提醒
import { ElMessage} from 'element-plus';
//1. 创建新的axios实例,
const service = axios.create({
// 公共接口 开发环境还是线上环境也可以用api
baseURL: process.env.NODE_ENV==='production' ? process.env.BASE_API : "/api",
// 超时时间 单位是ms,这里设置了5s的超时时间
timeout: 5000
})
// 2.请求拦截器
service.interceptors.request.use(config => {
//发请求前做的一些处理,数据转化,配置请求头,设置token,设置loading等,根据需求去添加
/*config.data = JSON.stringify(config.data); //数据转化,也可以使用qs转换
config.headers = {
'Content-Type':'application/x-www-form-urlencoded' //配置请求头
}
//注意使用token的时候需要引入cookie方法或者用本地localStorage等方法,推荐js-cookie
const token = getCookie('名称');//这里取token之前,你肯定需要先拿到token,存一下
if(token){
config.params = {'token':token} //如果要求携带在参数中
config.headers.token= token; //如果要求携带在请求头中
}*/
return config
}, error => {
Promise.reject(error)
})
// 3.响应拦截器
service.interceptors.response.use(response => {
//接收到响应数据并成功后的一些共有的处理,关闭loading等
return response
}, error => {
/***** 接收到异常响应的处理开始 *****/
if (error && error.response) {
// 1.公共错误处理
// 2.根据响应码具体处理
switch (error.response.status) {
case 400:
Message.error = '错误请求'
break;
case 401:
Message.error = '未授权,请重新登录'
break;
case 403:
Message.error = '拒绝访问'
break;
case 404:
Message.error = '请求错误,未找到该资源'
window.location.href = "/NotFound"
break;
case 405:
Message.error = '请求方法未允许'
break;
case 408:
Message.error = '请求超时'
break;
case 500:
Message.error = '服务器端出错'
break;
case 501:
Message.error = '网络未实现'
break;
case 502:
Message.error = '网络错误'
break;
case 503:
Message.error = '服务不可用'
break;
case 504:
Message.error = '网络超时'
break;
case 505:
Message.error = 'http版本不支持该请求'
break;
default:
Message.error = `连接错误${error.response.status}`
}
} else {
// 超时处理
if (JSON.stringify(error).includes('timeout')) {
Message.error('服务器响应超时,请刷新当前页')
}
Message.error('连接服务器失败')
}
/***** 处理结束 *****/
//如果不需要错误处理,以上的处理过程都可省略
return Promise.resolve(error.response)
})
//4.导出
export default service;
四、封装请求-http.js
import request from "./axios";
import qs from "qs";
// json格式请求头
const headerJSON = {
"Content-Type": "application/json",
};
// FormData格式请求头
const headerFormData = {
"Content-Type": "application/x-www-form-urlencoded;charset=UTF-8",
};
const http = {
/**
* methods: 请求
* @param url 请求地址
* @param params 请求参数
* @param json 判断数据发送是否是json格式
*/
get(url, params,json) {
const config = {
method: "get",
url: url,
headers: json ? headerJSON : headerFormData
};
if (params) config.params = params;
return request(config);
},
post(url, params,json) {
const config = {
method: "post",
url: url,
headers: json ? headerJSON : headerFormData
};
if (params) config.data = json?params: qs.stringify(params);
return request(config);
},
put(url, params,json) {
const config = {
method: "put",
url: url,
headers: json ? headerJSON : headerFormData
};
if (params) config.params = params;
return request(config);
},
delete(url, params,json) {
const config = {
method: "delete",
url: url,
headers: json ? headerJSON : headerFormData
};
if (params) config.params = params;
return request(config);
},
};
//导出
export default http;
五、正式封装API,用于发送请求-api.js
import http from '../utils/http'
//
/**
* @parms resquest 请求地址 例如:http://197.82.15.15:8088/request/...
* @param '/testIp'代表vue-cil中config,index.js中配置的代理
*/
let resquest = "/testIp/request/"
// get请求
export function getListAPI(params){
return http.get(`${resquest}/getList.json`,params,false)
}
// post请求
export function postFormAPI(params){
return http.post(`${resquest}/postForm.json`,params,false)
}
// put 请求
export function putSomeAPI(params){
return http.put(`${resquest}/putSome.json`,params,false)
}
// delete 请求
export function deleteListAPI(params){
return http.delete(`${resquest}/deleteList.json`,params,false)
}
六、在vue文件使用
import {getListAPI,postFormAPI, putSomeAPI, deleteListAPI} from '@/api/api';
//import api from '@/api/api' //全部导出
methods: {
//promise调用,链式调用, getList()括号内只接受参数;
// get不传参
getList() {
getListAPI().then(res => console.log(res)).catch(err => console.log(err))
},
//post传参
postForm(formData) {
let data = formData
postFormAPI(data).then(res => console.log(res)).catch(err => console.log(err))
},
//async await同步调用
async postForm(formData) {
const postRes = await postFormAPI(formData)
const putRes = await putSomeAPI({data: 'putTest'})
const deleteRes = await deleteListAPI(formData.name)
// 数据处理
console.log(postRes);
console.log(putRes);
console.log(deleteRes);
},
}