-
Notifications
You must be signed in to change notification settings - Fork 0
/
ajax-util.js
45 lines (38 loc) · 1.35 KB
/
ajax-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
37
38
39
40
41
42
43
44
45
(
function(global) {
var ajaxUtils ={};
function getRequestObject() {
if (window.XMLHttpRequest)
return ( new XMLHttpRequest() ) ;
else {
console.log ("AJAX not supported");
return(null);
}
}
ajaxUtils.sendGetRequest =
function(requestUrl, responseHandler, isJsonResponse) {
var request = getRequestObject();
request.onreadystatechange =
function() {
handleResponse(request,
responseHandler, isJsonResponse);
};
request.open("GET", requestUrl, true);
request.send(null);
};
function handleResponse (request, responseHandler, isJsonResponse) {
if ((request.readyState == 4) && (request.status == 200)) {
if(isJsonResponse == undefined) {
isJsonResponse = true;
}
if (isJsonResponse) {
responseHandler(JSON.parse(request.responseText))
}
else {
responseHandler(request.responseText);
}
};
}
global.$ajaxUtils = ajaxUtils;
}
)(window);