<SCRIPT type = "text/JavaScript">
VaR XMLHTTP;
Function createxmlhttprequest (){
If (window. activexobject ){
XMLHTTP = new activexobject ("Microsoft. XMLHTTP ");
}
Else if (window. XMLHttpRequest ){
XMLHTTP = new XMLHttpRequest ();
}
}
Function startrequest (){
Createxmlhttprequest ();
XMLHTTP. onreadystatechange = handlestatechange;
////!!!!! I have doubts about this place !!!!!!! //////////
XMLHTTP. Open ("get", "simpleresponse. xml", true );
XMLHTTP. Send (null );
}
Function handlestatechange (){
If (XMLHTTP. readystate = 4 ){
If (XMLHTTP. Status = 200 ){
Alert ("the server replied with:" + XMLHTTP. responsetext );
}
}
}
</SCRIPT>
//////////////////////////////////
My problem is:
XMLHTTP. onreadystatechange = handlestatechange;
Indicates that the handlestatechange method is called to return a status to XMLHTTP. onreadystatechange.
I think so, right?
Can I execute the following open () Send () after the status is available?
The last thing I don't understand is that since the handlestatechange method is executed
Why?
Do not write handlestatechange (); write handlestatechange;
Third question
Now that you want to return to XMLHTTP. onreadystatechange
Why is there no return in the handlestatechange () method? Does he know what to return?
Answer:
Question1: handlestatechange is a callback function. Because Ajax is an asynchronous method, this non-blocking running mechanism requires that a callback function be provided to the server for calling. That is to say, after you send a request to the server through a URL, after the server completes data processing, it will call the callback function you provide to it to return the data to you. Therefore, this method is not called for you, but for the server.
Q2: Why write handlestatechange instead of handlestatechange ()? This is a simple attempt. All we need is to declare the function entry, instead of calling the function. It is easy to understand, handlestatechange refers to the address of the handlestatechange () function.
Q3: see Q1. Since it is a callback function, the server has completed the work, and the rest is your function. Naturally, no return is required, you only need to receive the data returned by the server in the handlestatechange function for business processing. In this step, you have completed the communication with the server and do not need to return