Ajax技术简单入门

随着Google公司推出的Gmail服务后,越来越多的人开始关注Ajax技术了,所谓Ajax(Asynchronous JavaScript and XML缩写)技术,就是指运用JavaScript和XML在不用刷新Web页的情况下与Web服务器通信的技术.
一般来说,使用Ajax技术主要有两个原因:一是fast;二是cool。
下面通过一个示例来说明Ajax的使用:
1.HTML代码
btn1用来调用Ajax代码(请求服务器并将返回信息填充到select1里)。

1  < select  id ="select1" >< SPAN>select>
2 <input id="btn1" value="Fill Select" type="button" onclick="getOptions();">

2.JavaScript代码调用Ajax

 1 None.gif //  Create the Request object (the AJAX wrapper)
 2 None.gif var  request  =   new  Request();
 3 None.gif //  Change this to fit your environment
 4 None.gif var  url  =   " http://localhost/ajax/ " ;
 5 None.gif function  getOptions()
 6 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 7InBlock.gif    // Call the AJAX
 8InBlock.gif    // Notice the second parameter is actually a function to handle the response
 9InBlock.gif    request.GetNoCache(url + "requests/getOptions.aspx",
10InBlock.gif    function(result)
11ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
12InBlock.gif        if (result.readyState!=ReadyState.Complete)
13InBlock.gif            return;               
14InBlock.gif        if (result.status==HttpStatus.OK && result.responseText != "")
15ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
16InBlock.gif            // If the request was successfull and returned data
17InBlock.gif            var vals = result.responseText.split("~");
18InBlock.gif            for (i=0; i<vals.length; i++)
19ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
20InBlock.gif                var pair = vals[i].split("|");
21InBlock.gif                var op = new Option(pair[1], pair[0], falsefalse);
22InBlock.gif                var sel = document.getElementById("select1");
23InBlock.gif                sel.options[sel.length] = op;
24ExpandedSubBlockEnd.gif            }

25InBlock.gif            alert("Remember that the new values in form" + 
26InBlock.gif                  " element 'select1' are not in viewstate." + 
27InBlock.gif                  " Code appropriately.");
28ExpandedSubBlockEnd.gif        }

29InBlock.gif        else
30ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
31InBlock.gif            // Handle the failure condition
32InBlock.gif            alert('Get options failed.');
33ExpandedSubBlockEnd.gif        }

34ExpandedSubBlockEnd.gif    }

35InBlock.gif    )
36ExpandedBlockEnd.gif}

3.aspx文件

1  <% @ Page language = " c# "  Codebehind = " getOptions.aspx.cs "  AutoEventWireupo = " false "  Inherits = " ajax.requests.getOptions "   %>
2  <% = result %>

4.codebehind代码

1 None.gif protected string result = string.Empty;
2 None.gifprivate void Page_Load(object sender, System.EventArgs e)
3 None.gif{
4 None.gif    for (int i=0; i < 10 ; i++)
5 None.gif    {
6 None.gif        result + = i.ToString()  + "|option " + i.ToString() + "~";
7 None.gif    }
8 None.gif    result  = result.Substring(0,  result.Length - 1); // to drop the last '~'
9 None.gif}

更多 Ajax
下载: 源文件 项目测试
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值