Usage of Ajax in. net

Source: Internet
Author: User
Ajax is not too short. Although it is controversial in some aspects, it is not a secret. Ajax cannot tell which language it belongs to, but the intersection between any language and JavaScript and XML. I think it is more appropriate to say that AJAX is a cross-cutting between any Web language and DHTML and XML.

The following sections only discuss IE.

Ajax is used in applications in three parts (personal opinion ):
1. Data (usually through the built-in ie ComponentsMicrosoft. XMLHTTP to get or send data);
2. Events (events refer to client events. If they are server events, Ajax makes no sense );
3. BIND (for the moment, bind it, or display it, usually through DHTML ).

As shown above, Ajax is used.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:Aaaaa

B. aspx acquisition < Div ID = " Myshow " />

< Script Language = " Javascript " >
VaR XMLHTTP =   New Activexobject ( " Microsoft. XMLHTTP " );
// Data transmission, flase is not asynchronous
XMLHTTP. Open ( " Get " , " A. 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 >

. aspx provides data
XMLHTTP. open ( " Get " , " . aspx " , true ); Request. 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 >

Put B. aspx in
XMLHTTP. Open ( " Get " , " A. aspx " , True ); Changed to XMLHTTP. Open ( " Get " , " A. aspx? Flag = 1 " , True );
11111111111111 of the data is obtained.
XMLHTTP. Open ("Get","A. aspx",True); Changed to XMLHTTP. Open ("Get","A. aspx? Flag = 2",True);
The data is obtained.22222222222222

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 not asynchronous
XMLHTTP. Open ( " Post " , " A. 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 ");

XMLHTTP. Send ("P1 = qwdqwdqwdqwd"); // Here is the data to be submitted by post.
}
</Script>

Generally, both post and get exist in a simulated request.
As long
XMLHTTP. Open ("Post","A. aspx",True);
LiA. Add the GET request to 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.

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.