Ajax communication principle XMLHttpRequest

Source: Internet
Author: User

Obviously, AJax is a technology that uses JavaScript scripts to access data.
AJAX enables asynchronous update of web pages. This means that the webpage is partially updated without reloading the entire webpage.
XMLHttpRequest is the key to AJAX
Currently, browsers support XMLHttpRequest objects (IE5 and IE6 use ActiveXObject ).
The readyState request to the backend has five statuses: 0: the server is not initialized, 1: The server connection has been established, 2 requests have been accepted, 3 requests are being processed, and 4 requests have been completed.
The onreadystatechange event is triggered every time the status is changed. The status has two statuses: 200: "OK", 404: "Page not found"
The following shows the Ajax front-end implementation code:
Copy codeThe Code is as follows:
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> No title page </title>
<Script type = "text/javascript">
Function getName (){
Var xmlhttp;
If (window. XMLHttpRequest ){
Xmlhttp = new XMLHttpRequest ();
} Else {
Xmlhttp = new ActiveXObject ("Microsoft. XMLHTTP ");
}
Xmlhttp. onreadystatechange = function (){
If (xmlhttp. readyState = 4 & xmlhttp. status = 200 ){
Alert ("Hello:" + xmlhttp. responseText );
}
}
Xmlhttp. open ("post", "Default. aspx? Id = gname ", true );
Xmlhttp. send ();
}
</Script>
</Head>
<Body>
<Form id = "form1" runat = "server">
<Div> <input id = "Button1" type = "button" value = "button" onclick = "getName ()"/> </p>
</Div>
</Form>
</Body>
</Html>

Background code:
Copy codeThe Code is as follows:
Protected void Page_Load (object sender, EventArgs e)
{
If (Request ["id"]! = Null)
{
Response. Write ("Zhang San ");
Response. End ();
}
}

Execution result: as shown in Figure

Download the attachment code. Next time, let's see how Jquery asynchronously requests data.

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.