I use Ajax a lot for my web projects but just now I want to fully understand it. I got into this problem.
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
}
Look at the code. In my logic I think we have to send request first then get response later. So why in ajax code we get response before send request. Am I missing something here? Thank you in advance!