-
Notifications
You must be signed in to change notification settings - Fork 0
/
xhr.js
95 lines (94 loc) · 3.91 KB
/
xhr.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
//Adds $.xhr and jQuery-like $.ajax methods to the prescribed namespace.
//Inspired from David Flanagans excellent cross-platform utils http://www.davidflanagan.com/javascript5/display.php?n=20-1&f=20/01.js
//Includes underscore.js _.each and _.extend methods
//modified to behave like jQuery's $.ajax(), not complete.
(function($) {
var win=window, 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;
var hasOwnProperty = Object.prototype.hasOwnProperty,
nativeForEach = Array.prototype.forEach;
var _each = function (o, fn, ctx) {
if (o == null) return;
if (nativeForEach && o.forEach === nativeForEach)
o.forEach(fn, ctx);
else if (o.length === +o.length) {
for (var i = 0, l = o.length; i < l; i++)
if (i in o && fn.call(ctx, o[i], i, o) === breaker) return;
} else {
for (var key in o)
if (hasOwnProperty.call(o, key))
if (fn.call(ctx, o[key], key, o) === breaker) return;
}
};
var _extend = function (o) {
_each(slice.call(arguments, 1), function (a) {
for (var p in a) if (a[p] !== void 0) o[p] = a[p];
});
return o;
};
$.xhr = function () {
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) {
continue;
}
}
return function () { };
};
$._xhrResp = function (xhr) {
switch (xhr.getResponseHeader("Content-Type").split(";")[0]) {
case "text/xml":
return xhr.responseXML;
case "text/json":
case "application/json":
case "text/javascript":
case "application/javascript":
case "application/x-javascript":
return win.JSON ? JSON.parse(xhr.responseText) : eval(xhr.responseText);
default:
return xhr.responseText;
}
};
$._formData = function (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('&');
};
$.ajax = function (o) {
var xhr = $.xhr(), timer, n = 0;
o = _extend({ userAgent: "XMLHttpRequest", lang: "en", type: "GET", data: null, dataType: "application/x-www-form-urlencoded" }, o);
if (o.timeout) timer = setTimeout(function () { xhr.abort(); if (o.timeoutFn) o.timeoutFn(o.url); }, o.timeout);
xhr.onreadystatechange = function () {
if (xhr.readyState == 4) {
if (timer) clearTimeout(timer);
if (xhr.status < 300) {
if (o.success) o.success($._xhrResp(xhr));
}
else if (o.error) o.error(xhr, xhr.status, xhr.statusText);
if (o.complete) o.complete(xhr, xhr.statusText);
}
else if (o.progress) o.progress(++n);
};
var url = o.url, data = null;
var isPost = o.type == "POST" || o.type == "PUT";
if (!isPost && o.data) url += "?" + $._formData(o.data);
xhr.open(o.type, url);
if (isPost) {
var isJson = o.dataType.indexOf("json") >= 0;
data = isJson ? JSON.stringify(o.data) : $._formData(o.data);
xhr.setRequestHeader("Content-Type", isJson ? "application/json" : "application/x-www-form-urlencoded");
}
xhr.send(data);
};
})(window);