forked from jhu-ep-coursera/fullstack-course4
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathajax-utils.js
More file actions
53 lines (41 loc) · 1.13 KB
/
Copy pathajax-utils.js
File metadata and controls
53 lines (41 loc) · 1.13 KB
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
(function (global) {
// Set up a namespace for our utility
var ajaxUtils = {};
// Returns an HTTP request object
function getRequestObject() {
if (window.XMLHttpRequest) {
return (new XMLHttpRequest());
}
else if (window.ActiveXObject) {
// For very old IE browsers (optional)
return (new ActiveXObject("Microsoft.XMLHTTP"));
}
else {
global.alert("Ajax is not supported!");
return(null);
}
}
// Makes an Ajax GET request to 'requestUrl'
ajaxUtils.sendGetRequest =
function(requestUrl, responseHandler) {
var request = getRequestObject();
request.onreadystatechange =
function() {
handleResponse(request, responseHandler);
};
request.open("GET", requestUrl, true);
request.send(null); // for POST only
};
// Only calls user provided 'responseHandler'
// function if response is ready
// and not an error
function handleResponse(request,
responseHandler) {
if ((request.readyState == 4) &&
(request.status == 200)) {
responseHandler(request);
}
}
// Expose utility to the global object
global.$ajaxUtils = ajaxUtils;
})(window);