forked from przemelek/TimeToRead
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.js
36 lines (34 loc) · 773 Bytes
/
util.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
var consumer_key = "<put here your key>";
function fetch(url,method,headers,body,callback,async,returnAsXML) {
var xhr = new XMLHttpRequest();
if (async) {
xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
callback(xhr.responseText);
} else {
callback(null);
}
}
}
}
xhr.open(method, url, async);
if (headers!==null) {
for (var i=0; i<headers.length; i++) {
xhr.setRequestHeader(headers[i].name,headers[i].value);
}
}
if (body!==null) {
xhr.send(body);
} else {
xhr.send();
}
if (!async) {
if (returnAsXML) {
//alert(xhr.responseText);
return xhr.responseXML;
} else {
return xhr.responseText;
}
}
};