03-http.js
4.82 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
/**
* auth : iMethod
* create_at: 15/10/13.
* desc:
* note:
* 1.
*/
(function ($w, $) {
$w.gxb = {}
$w.gxb._ = {}
$w.gxb.http = $w.gxb._.http = {
resloveUrl: function (url) {
return url;
// var noContextPath = $w.gxb.contextPath == "/";
// return (noContextPath ? "" : $w.gxb.contextPath) + (url.indexOf("/") == 0 ? "" : "/") + url;
},
GetQueryString: function (name) {
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
var r = window.location.search.substr(1).match(reg);
if (r != null)return unescape(r[2]);
return null;
}
}
$w.gxb._.ajaxs = {
_: {},
push: function (id, xhr) {
$w.gxb._.ajaxs._[id] = xhr;
},
pop: function (id) {
$w.gxb._.ajaxs._[id] = null;
},
get: function (id) {
return $w.gxb._.ajaxs._[id];
}
};
$w.gxb._.ajax = function (url, options) {
if (typeof url == "object") {
options = url;
url = null;
}
var def = {
url: url || options.url || "",
type: "get",
dateType: "html",
data: {}
};
options = $.extend(def, options);
url = options.url;
while (url[0] == "/" || url[0] == " ") {
url = url.substring(1, url.length);
}
if (url.length == 0) {
$w.console.log("无效的请求地址");
return null;
}
var newUrl = url.toLowerCase();
if (newUrl.indexOf(".js") < 0 && newUrl.indexOf(".css") < 0) {
var sIndex = newUrl.indexOf("?");
if (sIndex > 0) {
url = url.substring(0, sIndex) + "/api" + url.substring(sIndex)+"&"+new Date().getTime();
}
else {
url += "/api"+"?"+new Date().getTime()
}
}
var seq = function () {
var _id;
do {
_id = Math.random() + "00000000";
_id = _id.substr(_id.indexOf(".") + 1, 6);
} while (!!$w.gxb._seq[_id]);
$w.gxb._seq[_id] = !0;
return _id;
}
var params = {
url: $w.gxb._.http.resloveUrl(url),
data: options['data'] || {},
dateType: options['dateType'] || "html",
contentType: typeof(options["contentType"]) != 'undefined' && options["contentType"] != null ? options["contentType"] : "application/x-www-form-urlencoded;charset=utf-8",
type: options['type'] || 'get',
processData: typeof(options["processData"]) != 'undefined' && options["processData"] != null ? options['processData'] : true,
cache: typeof(options["cache"]) != 'undefined' && options["cache"] != null ? options['cache'] : true,
complete: function (response) {
var complete = options['complete'];
if (typeof complete == "function") {
complete(response)
}
$w.gxb._.ajaxs.pop(seq);
},
success: function (response) {
var success = options['success'];
if (typeof success == "function") {
success(response)
}
},
error: function (response) {
var error = options['error'];
var getResponse = function () {
if (response && response.status == 500) {
try {
var json = gxb.utils.toJsonObj(response.responseText);
}
catch (ex) {
var json = {};
}
return {
status: false,
message: json.message || "发生未知异常!",
type:json.type
}
}else if(response.status==403){
return {
status: false,
message: "您没有访问权限!"
}
}else {
return {
status: false,
message: "发生未知异常!"
}
}
}
if (typeof error == "function") {
error(getResponse(response));
} else {
var resData = getResponse(response)
console.log(resData.message)
}
}
};
var $ajax = $.ajax(params);
return {
abort: function () {
$ajax.abort();
}
};
};
})(window, jQuery);