An informal discussion on the use of Ajax in. Net

Source: Internet
Author: User

Ajax is not a short time to come out. Although it is controversial in some respects, it is afterward. Ajax cannot say which language it belongs to, but the intersection of any language with Javascript,xml. I think it's more appropriate to say that Ajax is the intersection of any Web language with DHTML and XML.

Only the IE section is discussed below.

Ajax is used in applications with 3 parts (personal point of view):
1, data (generally through IE built-in component microsoft.xmlhttp to obtain or send data);
2, the event (the event refers to the client event, if it is a server-side event, then Ajax also has no meaning);
3, binding (for the moment is called binding bar, it can be said to be shown, generally through DHTML to complete).

From the above, Ajax uses the Microsoft.XMLHTTP component and the DHTL. In fact, there is another part, is the server-side processing.

First, simple example
The simplest prototype is the acquisition of data:
The contents of a.aspx are as follows:
Aaaaab.aspx obtained
<div id= "Myshow"/>

<script language= "JavaScript" >
var xmlhttp= new ActiveXObject ("Microsoft.XMLHTTP");
Data transfer, flase for non-asynchronous mode
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>
A.aspx provides data
Xmlhttp.open ("Get", "a.aspx", true); the request is a.aspx.


if (xmlhttp.readystate==4) {
Myshow.innertext = Xmlhttp.responsetext;
}
When the asynchronous request completes, change the contents of the Myshow with DHML.

Two, 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 the b.aspx
Xmlhttp.open ("Get", "a.aspx", true); Change to Xmlhttp.open ("Get", "a.aspx?flag=1", true);
Then get Data 11111111111111
Xmlhttp.open ("Get", "a.aspx", true); Change to Xmlhttp.open ("Get", "a.aspx?flag=2", true);
Then get Data 22222222222222

Three, Post method
If you have such a form
<form method=post>
<input name= "P1" Type=text/>
<input name= "P2" type=submit/>
</form>
Using AJAX is
<div id= "Myshow"/>

<script language= "JavaScript" >
var xmlhttp= new ActiveXObject ("Microsoft.XMLHTTP");
Data transfer, flase for non-asynchronous mode
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 for post.
}
</script>

and general simulation requests are both post and get.
Just put
Xmlhttp.open ("Post", "a.aspx", true);
The A.aspx plus GET request section is OK.

And in. NET, you can write Ajax as a server component to use. Now in real projects, if you use a lot of Ajax, there is a special component to use. There is also the need to note that many times when Ajax to set the page does not cache. And if you want to be compatible with non IE kernel browsers, then you should pay attention to the various kernel browser JS is compatible.

Http://birdshover.cnblogs.com/archive/2006/07/03/441439.html

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.