I am having problems with an AJAX function I am working on.
The function is supposed to insert some information into a DB. The problem comes when I put the whole AJAX code inside a loop, because it crashes. Here is an example of a code which is similar to what I am doing:
function funcionDePrueba() {
var url = 'aaajax.py'
var x = 3
for (i = 0; i < x; i++) {
xmlhttp = GetXmlHttpObject();
if (!xmlhttp) {
alert("Browser does not support HTTP Request");
return;
}
var xml = xmlhttp;
xmlhttp.onreadystatechange = function() {
if (xml.readyState == 4) {
alert(claveProyecto);
}
};
xmlhttp.open("GET", url, true);
xmlhttp.send(null);
return true;
}
}
Is it possible to put the whole AJAX code inside a for loop?
Yes, it is possible.
But; In the provided snippet you have a
returnstatement inside yourfor-loop. This will make the loop have one go and then return from the function. If you’d like it do to more than one iteration, remove the statement:return true.