-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
105 lines (97 loc) · 3.71 KB
/
server.js
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
(function(){
var server = H.namespace('server');
var contextPath = "/community/";
/**
* 发起请求方法
* @param type{get|post} 请求类型
* @param api 请求地址 url
* @param parameters 请求发布参数
* @param success 回调方法,(错误也会调用)
* @param async 事后异步请求
* @returns {*} ajax对象
*/
var send = function (type, api, parameters, success, async) {
typeof success == 'function' || (success = function () {
});
var request = $.ajax({
url: api + "?r=" + Math.random(),
data: parameters,
type: type,
dataType: 'json',
async: true,
cache: false,
headers: {"Cache-Control": "no-cache", "Accept": "application/json"},
timeout: 300000,
success: function (data, textStatus, jqXHR) {
success.call(this, data, textStatus, jqXHR);
},
error: function (jqXHR, textStatus, errorThrown) {
//alert(jqXHR+errorThrown+textStatus);
if (jqXHR.status == 401) {
location.href = contextPath;
} else {
if (!errorThrown) {
return false;
}
var errors = {
101: "网络不稳定或不畅通,请检查网络设置",
403: "服务器禁止此操作!",
500: "服务器遭遇异常阻止了当前请求的执行<br/><br/><br/>"
};
var msg = null;
switch (textStatus) {
case "timeout":
msg = "网络连接超时,请检查网络是否畅通!";
break;
case "error":
if (errors[jqXHR.status]) {
var data = null;
try {
data = jQuery.parseJSON(jqXHR.responseText);
} catch (e) {
}
if (data && data.message) {
msg = data.message;
} else {
msg = errors[jqXHR.status];
}
} else {
msg = "服务器响应异常<br/><br/>" + (jqXHR.status == 0 ? "" : jqXHR.status) + " " + errorThrown;
}
break;
case "abort":
msg = null;//"数据连接已被取消!";
break;
case "parsererror":
msg = "数据解析错误!";
break;
default:
msg = "出现错误:" + textStatus + "!";
}
if (errorThrown.code != null && errorThrown.message != null && !errors[errorThrown.code]) {
msg += "</br>[code:" + errorThrown.code + "][message:" + errorThrown.message + "]" + (null == errorThrown.stack ? "" : errorThrown.stack);
}
if (msg == null) {
msg = '';
}
success.call(this, {code: jqXHR.status, msg: msg}, textStatus, jqXHR, errorThrown);
}
}
});
return request;
}
// 注册
server.regist = function (data, callback) {
//alert(data);
return send('post', '/api/register', data, callback);
}
// 登录
server.login = function (data, callback) {
//alert(data);
return send('post','/api/login', data, callback);
}
// 退出登录
server.logout = function (data, callback) {
return send('post', '/api/logout', data, callback);
}
})()