A simple example of AJAX ~ (Zt)

Source: Internet
Author: User
1. Ajax is short for Asynchronous JavaScript and XML, that is, Asynchronous JavaScript and XML. It is a technology that uses JavaScript, Dom, XML, CSS, and other technologies to implement asynchronous data transmission.
2. Using Ajax technology, you can achieve interaction with the background without refreshing new pages on the interface. More importantly, "Asynchronous" reduces the server load and reduces the waiting time of customers.
3. Ajax can be understood as a multithreading technology.
4. When the transmitted data volume is small and the XML unified project interface is not considered, you do not need to return XML.

The following is an example (. NET ):
Effect: select a person's name in the dropdownlist, search for the email of the person in the database based on the options in the background, and then display it in the textbox.
Download the most Ajax file from the www.schwarz-interactive.de.
Add reference ajaxpro.2.dll (. NET 2.0 ).
Add the following lines to Web. config: <? XML version = "1.0" encoding = "UTF-8" ?>
< Configuration >
< System . Web >
< Httphandlers >
< Add Verb = "Post, get" Path = "Ajaxpro/*. ashx" Type = "Ajaxpro. ajaxhandlerfactory, ajaxpro.2" />
</ Httphandlers >

[]

</ System. Web >
</ Configuration >

In. net Code To register the Ajax method: Public   Class _ Default
{
Protected   Void Page_load ( Object Sender, eventargs E)
{
Ajaxpro. Utility. registertypeforajax (Typeof(_ Default ));
}

[Ajaxpro. ajaxmethod]
Public   String Getemail ( String Name)
{

}
}

Note that typeof (_ default) is consistent with the class _ default class name above.
When the client sends an email request to the serverPublic StringGetemail (StringName) method response, use name to find the returned result email. (The Process Code is omitted here)
Note that if there are multiple Ajax methods, you must add the [ajaxpro. ajaxmethod] before each method.

This is done on the server side. Let's look at the client.
Write two functions in the script as follows: < Script Language = " Javascript " Type = " Text/JavaScript " >
Function Getemail ()
{
VaRName=Document. getelementbyid ("Dropdownlist1"). Value;
_ Default. getemail (name, getemail_callback );
}
Function Getemail_callback (RES)
{
Document. getelementbyid ("Email"). Value=Res. value;
}
</ Script >

The first function is the function called when an event is triggered. It means that the value of the element with ID "dropdownlist1" is obtained first, and then the Ajax method registered in class _ default is called getemail (), name isPublic StringGetemail (StringName), while getemail_callback is the client callback method. This method returns the result to the client when the server obtains the result. The second function getemail_callback (RES) is: when the server receives the result and calls this function, set the value of the element ID "email" to the value of the returned result res.
Note:
1. If getemail () does not have a parameter, write the callback function in brackets: _ default. getemail (getemail_callback );
2. The callback function does not need to be written in the format of "original function _ callback". Therefore, the callback function is written to facilitate differentiation.

Finally, write an onchange event in dropdownlost1 and call the getemail () function. < ASP: dropdownlist ID = "Dropdownlist1" Runat = "Server" Onchange = "Getemail ()" > </ ASP: dropdownlist >
< ASP: textbox ID = "Email" Runat = "Server" > </ ASP: textbox >

The simple example is complete.

PS: this stuff was found when I was looking for something called _ callback. I thought it was still clear, and I just switched to it ~
Original post address: http://www.cnblogs.com/jeffamy/archive/2006/04/23/382927.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.