-
Notifications
You must be signed in to change notification settings - Fork 641
/
dom_request_xhr.js
146 lines (140 loc) · 3.94 KB
/
dom_request_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
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
/*
----------------------------------------------------------
util/Request : 0.1.1 : 2015-03-26
----------------------------------------------------------
util.request({
url: './dir/something.extension',
data: 'test!',
format: 'text', // text | xml | json | binary
responseType: 'text', // arraybuffer | blob | document | json | text
headers: {},
withCredentials: true, // true | false
///
onerror: function(evt, percent) {
console.log(evt);
},
onsuccess: function(evt, responseText) {
console.log(responseText);
},
onprogress: function(evt, percent) {
percent = Math.round(percent * 100);
loader.create('thread', 'loading... ', percent);
}
});
*/
if (typeof MIDI === 'undefined') MIDI = {};
(function(root) {
var util = root.util || (root.util = {});
util.request = function(opts, onsuccess, onerror, onprogress) { 'use strict';
if (typeof opts === 'string') opts = {url: opts};
///
var data = opts.data;
var url = opts.url;
var method = opts.method || (opts.data ? 'POST' : 'GET');
var format = opts.format;
var headers = opts.headers;
var responseType = opts.responseType;
var withCredentials = opts.withCredentials || false;
///
var onsuccess = onsuccess || opts.onsuccess;
var onerror = onerror || opts.onerror;
var onprogress = onprogress || opts.onprogress;
///
if (typeof NodeFS !== 'undefined' && root.loc.isLocalUrl(url)) {
NodeFS.readFile(url, 'utf8', function(err, res) {
if (err) {
onerror && onerror(err);
} else {
onsuccess && onsuccess({responseText: res});
}
});
return;
}
///
var xhr = new XMLHttpRequest();
xhr.open(method, url, true);
///
if (headers) {
for (var type in headers) {
xhr.setRequestHeader(type, headers[type]);
}
} else if (data) { // set the default headers for POST
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
}
if (format === 'binary') { //- default to responseType="blob" when supported
if (xhr.overrideMimeType) {
xhr.overrideMimeType('text/plain; charset=x-user-defined');
}
}
if (responseType) {
xhr.responseType = responseType;
}
if (withCredentials) {
xhr.withCredentials = 'true';
}
if (onerror && 'onerror' in xhr) {
xhr.onerror = onerror;
}
if (onprogress && xhr.upload && 'onprogress' in xhr.upload) {
if (data) {
xhr.upload.onprogress = function(evt) {
onprogress.call(xhr, evt, event.loaded / event.total);
};
} else {
xhr.addEventListener('progress', function(evt) {
var totalBytes = 0;
if (evt.lengthComputable) {
totalBytes = evt.total;
} else if (xhr.totalBytes) {
totalBytes = xhr.totalBytes;
} else {
var rawBytes = parseInt(xhr.getResponseHeader('Content-Length-Raw'));
if (isFinite(rawBytes)) {
xhr.totalBytes = totalBytes = rawBytes;
} else {
return;
}
}
onprogress.call(xhr, evt, evt.loaded / totalBytes);
});
}
}
///
xhr.onreadystatechange = function(evt) {
if (xhr.readyState === 4) { // The request is complete
if (xhr.status === 200 || // Response OK
xhr.status === 304 || // Not Modified
xhr.status === 308 || // Permanent Redirect
xhr.status === 0 && root.client.cordova // Cordova quirk
) {
if (onsuccess) {
var res;
if (format === 'xml') {
res = evt.target.responseXML;
} else if (format === 'text') {
res = evt.target.responseText;
} else if (format === 'json') {
try {
res = JSON.parse(evt.target.response);
} catch(err) {
onerror && onerror.call(xhr, evt);
}
}
///
onsuccess.call(xhr, evt, res);
}
} else {
onerror && onerror.call(xhr, evt);
}
}
};
xhr.send(data);
return xhr;
};
/// NodeJS
if (typeof module !== 'undefined' && module.exports) {
var NodeFS = require('fs');
XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest;
module.exports = root.util.request;
}
})(MIDI);