Ajax is used in applications in three parts (personal opinion ):
1. Data (generally, data is obtained or sent through Microsoft. XMLHTTP, a built-in ie component );
2. Events (events refer to client events. If they are server events, Ajax makes no sense );
3. BIND (for the time being, bind it, or display it. It is usually done through DHTML ).
As shown above, Ajax uses the Microsoft. XMLHTTP component and dhtl. Another part is server-side processing.
1. Simple Example
The simplest prototype is to obtain data:
A. The content of aspx is as follows:
<Div id = "myshow"/> <script language = "JavaScript"> var XMLHTTP = new activexobject ("Microsoft. XMLHTTP "); // data transmission. flase is non-asynchronous XMLHTTP. open ("get", ". aspx ", true); XMLHTTP. onreadystatechange = function () {If (XMLHTTP. readystate = 4) {myshow. innertext = XMLHTTP. responsetext;} If (XMLHTTP. readystate = 3) {myshow. innertext = ('submitting data') ;}} XMLHTTP. send (null) ;}</SCRIPT>
A. aspx provides data XMLHTTP. Open ("get", "a. aspx", true); that is, requests a. aspx.
if (xmlhttp.readyState==4) {MyShow.InnerText = xmlhttp.responseText;} |
When the asynchronous request is complete, use dhml to change the content of myshow.
Ii. Get Method
Change a. aspx as follows:
< script runat="Server" language="C#">string flag = Request["flag"] == null ? "" : Request["flag"];switch(flag){case "1":Response.Write("11111111111111");break;case "2" :Response.Write("22222222222222");break;}< /script> |
Change XMLHTTP. Open ("get", "a. aspx", true) in B. aspx to XMLHTTP. Open ("get", "a. aspx? Flag = 1 ", true );
11111111111111 of the data is obtained.
XMLHTTP. Open ("get", "a. aspx", true); Modify
XMLHTTP. Open ("get", "a. aspx? Flag = 2 ", true );
22222222222222 of the data is obtained.
Iii. Post Method
If there is such a form
< form method=post>< input name="p1" type=text />< input name="p2" type=submit/>< /form> |
Ajax is
<Div id = "myshow"/> <script language = "JavaScript"> var XMLHTTP = new activexobject ("Microsoft. XMLHTTP "); // data transmission. flase is non-asynchronous XMLHTTP. open ("Post", ". aspx ", true); XMLHTTP. onreadystatechange = function () {If (XMLHTTP. readystate = 4) {myshow. innertext = XMLHTTP. responsetext;} If (XMLHTTP. readystate = 3) {myshow. innertext = ('submitting data') ;}} XMLHTTP. setRequestHeader ("Content-Type", "application/X-WWW-form-urlencoded"); x Mlhttp. Send ("p1 = qwdqwdqwdqwd"); // here is the data to be submitted by post .} </SCRIPT>
Generally, both post and get requests exist simultaneously. You only need to add the GET request part to XMLHTTP. Open ("Post", "a. aspx", true); A. aspx.
In. net, AJAX can be used as a server component. In actual projects, if Ajax is used in many cases, a dedicated component is available. In addition, you must note that in most cases, you need to set the page not to cache in Ajax. If you want to be compatible with non-ie kernel browsers, pay attention to whether the Js of various kernel browsers is compatible.