Record a piece of basic Ajax writing and add comments to the code. Many people think it is mysterious, but they can understand it after reading it.
Here, the. NET aspx is used as the server for return processing.
JS Code, call Method
<SCRIPT type = "text/JavaScript">
VaR request = false;
Try ...{
Request = new XMLHttpRequest (); // non-Microsoft browser
} Catch (trymicrosoft )...{
Try ...{
Request = new activexobject ("msxml2.xmlhttp"); // Microsoft IE
} Catch (othermicrosoft )...{
Try ...{
Request = new activexobject ("Microsoft. XMLHTTP"); // other Microsoft
} Catch (failed )...{
Request = false;
}
}
}
If (! Request) // The old browser does not support the XMLHTTPRequest object
Alert ("error initializing XMLHttpRequest! ");
Function getcustomerinfo ()...{
VaR phone = Document. getelementbyid ("text1"). value; // obtain the text touch value on the form.
VaR url = "http: // localhost: 14379/webhtmlpartdynamic/default2.aspx? Phone = "+ escape (phone); // requested webpage
Request. Open ("get", URL, true); // create a request: Get method, address, true: asynchronous call
Request. onreadystatechange = updatepage; // method called after the server completes Processing
Request. Send (null); // send the request (generally, the security information is not sent and XML is null)
}
Function updatepage ()...{
If (request. readystate = 4) // determine the HTTP request readiness status. Here, 4 is the last status.
If (request. Status = 200) // The normal Request status code is 200 (returns a 404-like representation of the error connection)
...{
VaR DH = request. responsetext;
Alert (DH );
}
Else if (request. Status = 404) // The error check is as follows:
Alert ("request URL does not exist ")
Else
Alert ("error: status code is" + request. status );
}
</SCRIPT>
How to call HTML code
<Body>
<Form ID = "form1" runat = "server">
<Div>
<Input id = "text1" type = "text"/>
<Input id = "submit1" type = "Submit" value = "Submit" onclick = "getcustomerinfo ()"/> </div>
</Form>
</Body>
Written in aspx as the processing part in response to Ajax requests
Protected void page_load (Object sender, eventargs E)
...{
If (! Ispostback)
...{
String STR = request. querystring ["phone"];
Response. Write (STR );
Response. Flush ();
Response. End ();
}
}