forked from whgojp/JavaSecLab
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjquery.request.js
More file actions
60 lines (57 loc) · 1.89 KB
/
jquery.request.js
File metadata and controls
60 lines (57 loc) · 1.89 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
/**
* @description ajax请求统一封装
* @author: whgojp
* @email: whgojp@foxmail.com
* @Date: 2024/5/18 16:09
*/
const timeout = 10000; // 延长至10s 方便测试延时注入
// requet ajax
function request (url, method, data = {}, contentType, back){
$.ajax({
url: url,
type: method,
data: data,
cache: false,
timeout: timeout,
contentType: contentType,
xhrFields: {
withCredentials: true,
},
success: function(res, textStatus, xhr){
console.log(this.type+' 请求成功:', {
url: this.url,
method: this.type,
data: this.data,
contentType: this.contentType,
response: res,
status: xhr.status,
statusText: xhr.statusText,
headers: xhr.getAllResponseHeaders()
});
return typeof back === "function" && back(res);
},
error: function(xhr, status, error) {
console.log(this.type+' 请求失败:', {
url: this.url,
method: this.type,
data: this.data,
contentType: this.contentType,
status: xhr.status,
statusText: xhr.statusText,
error: error,
headers: xhr.getAllResponseHeaders()
});
return typeof back === "function" && back(null);
}
});
};
function getAjaxRequst (url, data, callBack) {
request(url, "GET", data, "application/x-www-form-urlencoded;charset=UTF-8", function(res) {
return typeof callBack == "function" && callBack(res)
})
};
function postAjaxRequst (url, data, callBack) {
request(url, "POST", data, "application/x-www-form-urlencoded;charset=UTF-8", function(res) {
return typeof callBack == "function" && callBack(res)
})
};