1. Define an AJAX request object, set the method for receiving the returned results, and asynchronously request a webpage from the server.
2. Define a webpage for AJAX requests (return result string)
3. Define the feedback method, receive the AJAX request and return the result string (called by the system), and update somewhere on the page!
Detailed code of the three steps (three controls, two TextBox, and one button are used)
<Script language = javascript>
Var xmlObj = null;
Function do_ajax_request ()
{// Defines an AjAx request object, sends an Ajax request asynchronously to the server (requesting a page), and specifies a feedback method for receiving returned results
If (window. XMLHttpRequest)
{
XmlObj = new XMLHttpRequest ();
}
Else if (window. ActiveXObject)
{
XmlObj = new ActiveXObject ("Microsoft. XMLHTTP ");
}
Else
{
Alert ("your IE version is too old, please change your IE ");
Return;
}
// Specify a feedback method for receiving returned results
XmlObj. onreadystatechange = do_handle_ajax;
// Send Ajax requests asynchronously to the server (request a page)
XmlObj. open ('get', 'Get _ talkmsg. aspx ', true); // get_talkmsg.aspx is a page
XmlObj. send ('');
}
Function do_handle_ajax ()
{
If (xmlObj. readyState = 4) // if the status is 4, the returned result is
{
// Alert (xmlObj. responseText );
Document. all ("TextBox_all"). value = xmlObj. responseText;
// Alert (xmlObj. responseText );
}
}
SetInterval (do_ajax_request, 1000 );
</Script>
// Get_talkmsg.aspx LOAD event
Protected void Page_Load (object sender, EventArgs e)
{
Response. ContentType = "text/plain ";
Response. Write (Application ["msg"]. ToString ());
Response. End ();
}