Ajax-onreadystatechange Event (properties of the XMLHttpRequest object)

Source: Internet
Author: User

Three important attributes of the XMLHttpRequest object:

Properties Description
onReadyStateChange The function (or function name) is called whenever the ReadyState property is changed.
ReadyState

The state of being xmlhttprequest. Vary from 0 to 4.

  • 0: Request not initialized
  • 1: Server Connection established
  • 2: Request received
  • 3: In Request processing
  • 4: The request is complete and the response is ready
Status

$: "OK"

404: Page Not Found

Example:

1 xmlhttp.onreadystatechange=function () 2   {3  if (xmlhttp.readystate==4 && xmlhttp.status==200)4     {5    document.getElementById ("mydiv"). Innerhtml=xmlhttp.responsetext; 6     }7   }

When ReadyState equals 4 and the status is 200, the response is ready (note: The onReadyStateChange event is triggered 5 times (0-4), corresponding to each change of the readyState. )

Using the Callback function
The callback function is a function that is passed as an argument to another function.

Example:

1 <Scripttype= "Text/javascript">2 varXMLHTTP;3 functionLoadxmldoc (url,cfunc)/ * Pass the parameter, the first parameter represents the URL address, and the second parameter represents the function to execute *  /4 {5 if(window. XMLHttpRequest)6   {//code for ie7+, Firefox, Chrome, Opera, Safari7 XMLHTTP=NewXMLHttpRequest ();8   }9 ElseTen   {//code for IE6, IE5 One XMLHTTP=NewActiveXObject ("Microsoft.XMLHTTP"); A   } - Xmlhttp.onreadystatechange=Cfunc; / * Call function *  / - Xmlhttp.open ("GET", URLs,true); the xmlhttp.send (); - } - functionmyFunction () - { + Loadxmldoc ("/ajax/test1.txt",function() -   { +   if(Xmlhttp.readystate==4 &&Xmlhttp.status== $) A     { at document.getElementById ("mydiv"). InnerHTML=Xmlhttp.responsetext; -     } -   }); - } - </Script>

Ajax-onreadystatechange Event (properties of the XMLHttpRequest object)

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.