httpx.js
2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import axios from "axios";
import { MessageBox } from 'element-ui'
const httpx = axios.create({
timeout: 8000 // 请求超时时间
});
/****** 数据请求之前******/
httpx.interceptors.request.use(config => {
console.log("数据请求之前.....")
return config;
}, error => {
//请求错误处理
const txt = `本页数据请求之前中存在错误 :【${error}】
地址:【${httpx.baseURL}】
您访问的页面资源可能不存在,或者不支持跨域,请核实,
点击任意空白处继续或刷新浏览器,问题频繁出现请联系管理员。`
MessageBox.alert(txt, "提示")
Promise.reject(error)
});
/****** respone拦截器==>对响应做处理 ******/
httpx.interceptors.response.use(response => {
console.log("数据请求成功之后...")
console.log(response)
if (response.data.code === 200) return response.data;
const txt = `本页数据请求成功之后返回结果 :【${response.data.msg}】
地址:【${response.config.url}】
请求方式:【${response.config.method}】
您访问的页面资源可能不存在,或者不支持跨域,请核实,
点击任意空白处继续或刷新浏览器,问题频繁出现请联系管理员。`
MessageBox.alert(txt, "提示");
}, error => {
var msg = JSON.stringify(error);
const txt = `本页数据请求成功之后中存在错误 :【${msg}】
地址:【${httpx.baseURL}】
您访问的页面资源可能不存在,或者不支持跨域,请核实,
点击任意空白处继续或刷新浏览器,问题频繁出现请联系管理员。`
MessageBox.alert(txt, "提示")
return Promise.reject(error)
});
/**
* 请求案例:
* 回调函数 嵌套回调焊 methods对应的方法加 async
let res = await service({
url: newUlr,
method: "get",
})
2:单个请求
service({
url: newUlr,
method: "get",
}).then((res) => {
})
*/
export default httpx