-
Notifications
You must be signed in to change notification settings - Fork 99
/
jquip.ajax.js
154 lines (139 loc) · 5.44 KB
/
jquip.ajax.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
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
$['plug']("ajax", function ($) {
var xhrs = [
function () { return new XMLHttpRequest(); },
function () { return new ActiveXObject("Microsoft.XMLHTTP"); },
function () { return new ActiveXObject("MSXML2.XMLHTTP.3.0"); },
function () { return new ActiveXObject("MSXML2.XMLHTTP"); }
],
_xhrf = null;
function _xhr() {
if (_xhrf != null) return _xhrf();
for (var i = 0, l = xhrs.length; i < l; i++) {
try {
var f = xhrs[i], req = f();
if (req != null) {
_xhrf = f;
return req;
}
} catch (e){}
}
return function () { };
} $['xhr'] = _xhr;
function _xhrResp(xhr, dataType) {
dataType = (dataType || xhr.getResponseHeader("Content-Type").split(";")[0]).toLowerCase();
if (dataType.indexOf("json") >= 0){
var j = false;
if(window.JSON){
j = window.JSON['parse'](xhr.responseText);
}else{
j = eval(xhr.responseText);
}
return j;
}
if (dataType.indexOf("script") >= 0)
return eval(xhr.responseText);
if (dataType.indexOf("xml") >= 0)
return xhr.responseXML;
return xhr.responseText;
} $['_xhrResp'] = _xhrResp;
$['formData'] = function formData(o) {
var kvps = [], regEx = /%20/g;
for (var k in o) kvps.push(encodeURIComponent(k).replace(regEx, "+") + "=" + encodeURIComponent(o[k].toString()).replace(regEx, "+"));
return kvps.join('&');
};
$['each']("ajaxStart ajaxStop ajaxComplete ajaxError ajaxSuccess ajaxSend".split(" "), function(i,o){
$['fn'][o] = function(f){
return this['bind'](o, f);
};
});
function ajax(url, o) {
var xhr = _xhr(), timer, n = 0;
if (typeof url === "object") o = url;
else o['url'] = url;
o = $['_defaults'](o, { 'userAgent': "XMLHttpRequest", 'lang': "en", 'type': "GET", 'data': null, 'contentType': "application/x-www-form-urlencoded", 'dataType': null, 'processData': true, 'headers': {"X-Requested-With": "XMLHttpRequest" }, 'cache': true });
if (o.timeout) timer = setTimeout(function () { xhr.abort(); if (o.timeoutFn) o.timeoutFn(o.url); }, o.timeout);
var cbCtx = $(o['context'] || document), evtCtx = cbCtx;
xhr.onreadystatechange = function() {
if (xhr.readyState == 4){
if (timer) clearTimeout(timer);
if (xhr.status < 300){
var res, decode = true, dt = o.dataType || "";
try{
res = _xhrResp(xhr, dt, o);
}catch(e){
decode = false;
if (o.error)
o.error(xhr, xhr.status, xhr.statusText);
evtCtx['trigger'](cbCtx, "ajaxError", [xhr, xhr.statusText, o]);
}
if (o['success'] && decode && (dt.indexOf('json')>=0 || !!res))
o['success'](res);
evtCtx['trigger'](cbCtx,"ajaxSuccess", [xhr, res, o]);
}
else {
if (o.error)
o.error(xhr, xhr.status, xhr.statusText);
evtCtx['trigger'](cbCtx, "ajaxError", [xhr, xhr.statusText, o]);
}
if (o['complete'])
o['complete'](xhr, xhr.statusText);
evtCtx['trigger'](cbCtx, "ajaxComplete", [xhr, o]);
}
else if (o['progress']) o['progress'](++n);
};
var url = o['url'], data = null;
var cache = o['cache']==true;
var isPost = o['type'] == "POST" || o['type'] == "PUT";
if( o['data'] && o['processData'] && typeof o['data'] == 'object' )
data = $['formData'](o['data']);
var d = url.indexOf('?') >= 0 ? '&' : '?'
if (!isPost && data) {
url += d + data;
data = null;
if(!cache)
url=url+"&_="+(new Date().getTime());
}else(!isPost && !cache)
url+=d+"_="+(new Date().getTime());
cache=null;
xhr.open(o['type'], url);
try {
for (var i in o.headers)
xhr.setRequestHeader(i, o.headers[i]);
} catch(_) { console.log(_) }
if (isPost) {
if(o['contentType'].indexOf('json')>=0)
data = o['data'];
xhr.setRequestHeader("Content-Type", o['contentType']);
}
xhr.send(data);
} $['ajax'] = ajax;
$['getJSON'] = function (url, data, success, error) {
if ($['isFunction'](data)){
error = success;
success = data;
data = null;
}
ajax({'url': url, 'data': data, 'success': success, 'dataType': 'json'});
};
$['get'] = function (url, data, success, dataType) {
if ($['isFunction'](data)) {
dataType = success;
success = data;
data = null;
}
ajax({'url': url, 'type': "GET", 'data': data, 'success': success, 'dataType': dataType || "text/plain"});
};
$['post'] = function (url, data, success, dataType) {
if ($['isFunction'](data)) {
dataType = success;
success = data;
data = null;
}
ajax({'url': url, 'type': "POST", 'data': data, 'success': success, 'dataType': dataType || "text/plain"});
};
$['getScript'] = function (url, success) {
return $['get'](url, undefined, success, "script");
};
if (!window.JSON)
$['loadAsync']("http://ajax.cdnjs.com/ajax/libs/json2/20110223/json2.js");
});