-
Notifications
You must be signed in to change notification settings - Fork 0
/
ajax.js
30 lines (29 loc) · 949 Bytes
/
ajax.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
// Generic Ajax GET Function, will call callFunction
// upon receipt of results. ajaxVars will be converted from JSON string to URL?key=value&key1=value1 sequence
function AJAX_GET(ajaxBaseUrl, ajaxVars, callFunction, callArgs) {
var xhttp;
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xhttp =new XMLHttpRequest();
} else { // code for IE6, IE5
xhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
callFunction(xhttp.responseText);
}
};
let counter = 0;
for (let key in ajaxVars) {
if (counter == 0) {
ajaxBaseUrl += '?';
} else {
ajaxBaseUrl += '&';
}
ajaxBaseUrl += encodeURIComponent(key) + '=' + encodeURIComponent(ajaxVars[key]);
counter++;
}
console.log("Ajax GET URL:", ajaxBaseUrl);
xhttp.open("GET", ajaxBaseUrl, true);
xhttp.send();
}