Analysis of an Ajax basic knowledge problem

Source: Internet
Author: User
<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

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.