I saw most of the AJAX examples in W3school look like this:
function loadDoc() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
document.getElementById("demo").innerHTML = xhttp.responseText; //get response
}
};
xhttp.open("GET", "ajax_info.txt", true);
xhttp.send(); //send request
}
I changed the order of the code like this:
function loadDoc() {
var xhttp = new XMLHttpRequest();
xhttp.open("GET", "ajax_info.txt", true);
xhttp.send(); //send request
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
document.getElementById("demo").innerHTML = xhttp.responseText; //get response
}
};
}
and it seems like it works too, and my brain will think second one more logic.
So my question is: Is there any rules on this AJAX code's order? Will it make any differences for the 2 example above in any situation?