forked from yanatan16/nanoajax
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
28 lines (23 loc) · 746 Bytes
/
index.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
exports.ajax = function (url, postBody, callback) {
if (!callback) callback = postBody, postBody = null
var req = getRequest()
if (!req) return callback(new Error('no request'))
req.onreadystatechange = function () {
if (req.readyState == 4)
callback(req.status, req.responseText)
}
if (postBody) {
req.open("POST", url, true)
req.setRequestHeader('X-Requested-With', 'XMLHttpRequest')
req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
} else {
req.open("GET", url, true)
}
req.send(postBody)
}
function getRequest() {
if (global.XMLHttpRequest)
return new global.XMLHttpRequest;
else
try { return new global.ActiveXObject("MSXML2.XMLHTTP.3.0"); } catch(e) {}
}