After completing my first Ajax function using online tutorials, such as Google and w3schools, I've successfully made it work. However, I'm struggling to grasp the logic behind it and would greatly appreciate an explanation!
Below is my complete code:
function loadPlayer(id)
{
var xmlhttp;
if (window.XMLHttpRequest)
xmlhttp=new XMLHttpRequest();
else if(window.ActiveXObject)
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
document.getElementById("newPlayer").innerHTML=xmlhttp.responseText;
};
xmlhttp.open("GET","http://localhost:8084/gmustudent/players?id=" + id,true);
xmlhttp.send();
}
My main concern revolves around the order in which I've written this code and how each statement is executed. Specifically, I am puzzled by the fact that I'm placing the response text in the newPlayer div within the onreadystatechange function before actually retrieving the data asynchronously from the URL.
I'm perplexed because I don't understand how you can insert the response text into the div without having retrieved it yet. Although it's functioning, the reason eludes me. Therefore, a simple explanation breaking down what exactly is happening here - particularly regarding the sequence of statements and their actions - would be incredibly helpful. Thank you in advance for your assistance!