General Ajax syntax

Source: Internet
Author: User

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 ();
}
}
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.