/
xhr-json.js
101 lines (82 loc) · 2.69 KB
/
xhr-json.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
// Simple XHR JSON REST library.
// Works on IE's >= IE9. ES5.
// (c) Raivo Laanemets 2013
// MIT license.
var XHRJSON = (function() {
var module = {
// Generic request start hook.
onstart: function() {},
// Generic request end hook.
onend: function() {},
// Generic error hook.
onerror: function() {}
};
// Executes the GET request.
// cb - called at end.
module.get = function(url, cb) {
request('GET', url, cb);
};
// Executes the POST request.
// cb - called at end.
module.post = function(url, object, cb) {
if (typeof object !== 'object' && !Array.isArray(object)) {
throw new Error('Data must be an object or an array.');
}
request('POST', url, cb, object);
};
// Executes the DELETE request.
// cb - called at end.
module.del = module['delete'] = function(url, cb) {
request('DELETE', url, cb);
};
// Executes the PUT request.
// cb - called at end.
module.put = function(url, object, cb) {
if (typeof object !== 'object' && !Array.isArray(object)) {
throw new Error('Data must be an object or an array.');
}
request('PUT', url, cb, object);
};
// Creates new HTTP request.
function request(method, url, cb, payload) {
var xhr = new XMLHttpRequest();
xhr.open(method, url, true);
xhr.onreadystatechange = ready(xhr, cb);
if (payload) {
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.send(JSON.stringify(payload));
} else {
xhr.send();
}
// Call the generic start hook.
module.onstart(xhr);
}
// Returns a function to use with
// built-in XHR's onreadystatechange.
function ready(xhr, cb) {
return function() {
if (xhr.readyState === 4) {
// Call the generic end hook.
module.onend(xhr);
if (xhr.status === 200) {
// Call the callback with the JSON from responseText.
if (typeof cb === 'function') {
cb(null, JSON.parse(xhr.responseText));
}
} else {
// Call the generic error hook.
module.onerror(xhr);
// Call the callback with error.
if (typeof cb === 'function') {
cb(new Error('Status code not 200.'));
}
}
}
};
}
return module;
})();
// CommonJS support.
if (typeof module !== 'undefined' && module.exports) {
module.exports = XHRJSON;
}