ajax-util.js
4.24 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/**
* Created by liuruijie on 2016/9/28.
* 前端控制
*/
//状态码
web_status = {
SUCCESS : "000",
FAIL : "001",
NO_LOGIN : "003",
NO_PRIVILEGE : "004"
};
function getQueryString(name) {
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
var r = window.location.search.substr(1).match(reg);
if (r != null) return unescape(r[2]);
return null;
}
function simpleSuccess(result) {
//如果成功,则读取后端返回的操作指令
if (result.status == web_status.SUCCESS) {
if(result['msg']){
alert(result.msg);
}
//刷新
if(result['refresh']){
window.location.reload();
return;
}
//返回
if(result['back']){
window.location.href = document.referrer;
}
//跳转
if(result['redirectUrl']!=null){
window.location.href = result.redirectUrl;
return;
}
return result.data;
}
//未登录
if (result.status == web_status.NO_LOGIN) {
alert("您还未登陆!");
window.location.href =
"http://127.0.0.1:8081/login.html?backToUrl="+encodeURIComponent(btoa(window.location.href));
}else{
//其他错误情况,直接弹出提示框
if(result.msg!=null){
alert(result.msg);
}
}
return null;
}
//对jquery的ajax方法再次封装
__ajax = function(url, data, success, type ,contentType){
success = success||function(data){};
data = data||{};
var config = {
url:url,
type:type,
dataType:"json",
data:data,
success:function(result){
success(simpleSuccess(result));
}
};
//如果需要token校验
if(contentType){
config.contentType = contentType;
}
var token = $.cookie("token");
if(token){
config.beforeSend = function (xhr) {
xhr.setRequestHeader("Authorization", "Basic " + btoa(token));
}
}
$.ajax(config)
};
//再再次封装
AJAX = {
GET:function(url, data, success){
__ajax(url, data, success, "get");
},
POST_JSON: function(url, data, success){
__ajax(url, data, success, "post", "application/json");
},
POST:function(url, data, success){
__ajax(url, data, success, "post");
},
DELETE: function(url, data, success){
__ajax(url, data, success, "delete");
},
PUT:function(url, data, success){
__ajax(url, data, success, "put", "application/json");
},
PATCH: function (url, data, success) {
__ajax(url, data, success, "patch", "application/json");
},
INCLUDE: function (url, id) {
$.ajax({
url:url,
type:"get",
dataType:"html",
error: function (code) {
$("#"+id).html("加载失败");
},
success: function (result) {
$("#"+id).html(result);
}
})
}
};
//
//
// function __act_ajax(url, data, success, type, contentType){
// if(!success&&type == 'get'){
// success = function(data){
// }
// }
// else if(!success){
// success = function(data){
// window.location.reload();
// }
// }
// var config = {
// url: url,
// data: data,
// type: type,
// dataType: "json",
// error: function(code){
// alert("失败! code = "+code.status);
// },
// success: function(result){
// success(result);
// }
// };
//
// if(contentType){
// config.contentType = contentType;
// }
// $.ajax(config);
// }
//
// ACT_AJAX = {
// GET:function(url, data, success){
// __act_ajax(url, data, success, "get");
// },
// POST:function(url, data, success){
// __act_ajax(url, data, success, "post");
// },
// PUT:function(url, data, success){
// __act_ajax(url, data, success, "put", "application/json");
// },
// DELETE:function(url, data, success){
// __act_ajax(url, data, success, "delete");
// },
// PATCH: function (url, data, success) {
// __act_ajax(url, data, success, "patch", "application/json");
// }
// };