After referencing MDN, it mentioned that by default, the JavaScript function will keep running until the server response is received.
This is essentially what AJAX is all about - Asynchronous JavaScript and XML.
Even though I have some experience with AJAX, I found myself a bit puzzled after reading that explanation. It made me question whether I truly understand the concept of AJAX. I do know that AJAX allows communication with the server without refreshing the page, meaning everything happens seamlessly in the background.
However, the scenario described in the reference got me thinking. For instance, if I have a piece of JavaScript code like this:
//true, hence the function runs while waiting for server response
var xmlResponse;
var url = "http://example.com/file.xml";
xml_req.open("GET", url, true);
xml_req.onreadystatechange = function() {
if(xml_req.readyState == 4 && xml_req.status == 200) {
if(xml_req.responseText != null)
xmlResponse = xml_req.responseXML; //server response may not yet arrive
else {
alert("failed");
return false;
}
};
xml_req.send(null);
Based on this, it seems like xmlResponse could potentially be undefined since the server is still busy fetching the data. Can someone please clarify how the execution flow works in AJAX technology? Your insights are greatly appreciated.