Get started with Ajax

Source: Internet
Author: User

With the Gmail service launched by Google, more and more people are paying attention to Ajax technology. The so-called Ajax (Asynchronous JavaScript and XML) technology, it refers to the technology that uses JavaScript and XML to communicate with Web servers without Refreshing Web pages.
Generally, there are two main reasons for using Ajax: fast and cool.
The following example shows how to use Ajax:
1. htmlCode
Btn1 is used to call Ajax code (request the server and fill the returned information in select1 ).

1 < Select ID = "Select1" >Select>
2 <InputID= "Btn1"Value= "Fill select"Type= "Button"Onclick= "Getoptions ();">

2. JavaScript code calls Ajax

1 // Create the request object (the Ajax wrapper)
2 VaR Request =   New Request ();
3 // Change this to fit your environment
4 VaR URL =   " Http: // localhost/ajax/ " ;
5 Function Getoptions ()
6 {
7 // Call the Ajax
8 // Notice the second parameter is actually a function to handle the response
9 Request. getnocache (URL +   " Requests/getoptions. aspx " ,
10 Function (Result)
11 {
12 If (Result. readystate ! = Readystate. Complete)
13 Return ;
14 If (Result. Status = Httpstatus. OK && Result. responsetext ! =   "" )
15 {
16 // If the request was successfull and returned data
17 VaR Vals = Result. responsetext. Split ( " ~ " );
18 For (I = 0 ; I < Vals. length; I ++ )
19 {
20 VaR Pair = Vals [I]. Split ( " | " );
21 VaR OP =   New Option (pair [ 1 ], Pair [ 0 ], False , False );
22 VaR Sel = Document. getelementbyid ( " Select1 " );
23 Sel. Options [SEL. Length] = OP;
24 }
25 Alert ( " Remember that the new values in Form "   +  
26 " Element 'select1' are not in viewstate. "   +  
27 " Code appropriately. " );
28 }
29 Else
30 {
31//Handle the failure condition
32Alert ('get options failed .');
33}
34 }
35 )
36 }

3. aspx File

1 <% @ Page Language = " C # " Codebehind = " Getoptions. aspx. CS " Autoeventwireupo = " False " Inherits = " Ajax. Requests. getoptions "   %>
2 <% = Result %>

4. codebehind code

1 Protected string result = string. empty;
2 Private void page_load (Object sender, system. eventargs E)
3 {
4 For (INT I = 0; I < 10 ; I ++)
5 {
6 Result + = I. tostring () + "| Option" + I. tostring () + "~ ";
7 }
8 Result = Result. substring (0, Result. Length-1); // to drop the last '~ '
9 }

More Ajax
Download: Source File Project Test

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.