This repository has been archived by the owner on Dec 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 353
/
xhr.js
75 lines (65 loc) · 2.17 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
(function(global) {
'use strict';
if (!('window' in global && 'document' in global))
return;
//----------------------------------------------------------------------
//
// XMLHttpRequest
// https://xhr.spec.whatwg.org
//
//----------------------------------------------------------------------
// XMLHttpRequest interface
// Needed for: IE7-
global.XMLHttpRequest = global.XMLHttpRequest || function() {
try { return new ActiveXObject("Msxml2.XMLHTTP.6.0"); } catch (_) { }
try { return new ActiveXObject("Msxml2.XMLHTTP.3.0"); } catch (_) { }
try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (_) { }
throw Error("This browser does not support XMLHttpRequest.");
};
// XMLHttpRequest interface constants
// Needed for IE8-
[
['UNSENT', 0],
['OPENED', 1],
['HEADERS_RECEIVED', 2],
['LOADING', 3],
['DONE', 4],
].forEach(function(p) { if (!(p[0] in global.XMLHttpRequest)) global.XMLHttpRequest[p[0]] = p[1]; });
// FormData interface
// Needed for: IE9-
(function() {
if ('FormData' in global)
return;
function FormData(form) {
this._data = [];
if (!form) return;
for (var i = 0; i < form.elements.length; ++i) {
var element = form.elements[i];
if (element.name !== '')
this.append(element.name, element.value);
}
}
FormData.prototype = {
append: function(name, value /*, filename */) {
if ('Blob' in global && value instanceof global.Blob)
throw TypeError("Blob not supported");
name = String(name);
this._data.push([name, value]);
},
toString: function() {
return this._data.map(function(pair) {
return encodeURIComponent(pair[0]) + '=' + encodeURIComponent(pair[1]);
}).join('&');
}
};
global.FormData = FormData;
var send = global.XMLHttpRequest.prototype.send;
global.XMLHttpRequest.prototype.send = function(body) {
if (body instanceof FormData) {
this.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
arguments[0] = body.toString();
}
return send.apply(this, arguments);
};
}());
}(self));