I'm trying to create a script that allows me to make a ajax call with few lines of code. The script works with one ajax request, but when it comes to handling multiple request at once it fails. What have I done wrong ?
The code only processes the last request, while leaving the others "loading ...".
Here's my code:
/****************
Related Javascript inside the HTML document
****************/
// First request
var ajax1 = new ajax_class();
ajax1.meth = "GET";
ajax1.file = "ajax_info.txt";
ajax1.elem = "results";
ajax1.send = null;
ajax1.ajax_call(ajax1.meth, ajax1.file, ajax1.elem, ajax1.send);
...
// Third request
var ajax3 = new ajax_class();
ajax3.meth = "GET";
ajax3.file = "ajax_info3.txt";
ajax3.elem = "results3";
ajax3.send = null;
ajax3.ajax_call(ajax3.meth, ajax3.file, ajax3.elem, ajax3.send);
/****************
Related HTML inside the HTML document
****************/
<body>
<div id="results">Nothing has happend yet for 1....</div>
<div id="results2">Nothing has happend yet for 2 ....</div>
<div id="results3">Nothing has happend yet for 3 ....</div>
</body>
/****************
Related code inside the JAVASCRIPT document
****************/
function ajax_class () {
this.meth = "GET";
this.file;
this.elem;
this.send = null;
this.ajax_call = function (meth, file, elem, send) {
x = new XMLHttpRequest();
x.onreadystatechange = function () {
if (x.readyState == 4 && x.status == 200) {
_id(elem).innerHTML = x.responseText;
}
else {
_id(elem).innerHTML = "Loading ...";
}
}
x.open(meth , file, true);
x.send(send);
}
}
Works now, just had to add the "var" in front of x variable