Ajax初探

作者花时间研究Ajax,做了两个测试程序,本文贴出无框架的例子。程序在VS2005Beta2的ASP.Net中完成,包含在Test1.aspx添加代码、新建MyHandler.ashx文件处理请求等步骤,还提到*.aspx、*.html文件也可处理请求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近,老听同事说起Ajax.今天花了一些时间,在网上读了一些相关帖子,自己试着做了两个测试程序,搞了一搞.一个是用Ajax.Net这个框架做的,另一个没用框架. Ajax.NetGuide已够详细,不用赘述.现将没有框架的例子程序贴到下面.对自己,是备忘.对别人(如果有兴趣的话),也不妨瞅上两眼,知道有Ajax这个东西.Google已经用到了它,SuggestMap.微软在下一代的Hotmail中也用到了这种技术,据说还将在Asp.Net2.0正式版中加入对它的支持.看样子,这个技术不可小觑之.不过,看网上的帖子,也有人对其骂之,说七宗罪之类.作为底层程序员,咱们管不了那么多,反正俗话说得好,艺不压身,学点新技术,总之是没有坏处的.废说不说了,言归正传.

程序是在VS2005Beta2ASP.Net中做的.

1.新建一个Website,将Default.aspx更名为Test1.aspx

Test1.aspx中加入如下代码:

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Test1.aspx.cs" Inherits="Test1" %>

 

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

 

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

    <title>Untitled Page</title>

    <script language="javascript">

    function makeRequest(url)

    {

        var httpRequest=false;

        if(window.XMLHttpRequest)

        {

            httpRequest=new XMLHttpRequest();

        }

        else

        {

            try

            {                

               httpRequest=new ActiveXObject("Msxml2.XMLHTTP");

            }

            catch(e)

            {

                try

                {

                    httpRequest=new ActiveXObject("Microsoft.XMLHTTP");

                }

                catch(e)

                {

                    alert("Sorry to can't create the XmlHttp instance within the browser.");                  

                }

            }

        }

        if(httpRequest)

        {

            httpRequest.onreadystatechange=myCallBack;

            httpRequest.open('get',url,true);

            httpRequest.send(null);

            return true;

        }

        else

            return false;

           

        function myCallBack()

        {           

            if(httpRequest.readyState==4)

            {

                if(httpRequest.status==200)

                {

                    alert(httpRequest.responseText);

                }

                else

                    alert("ResponseText Error!")               

            }          

        }

    }

    function myRequest()

    {

        var name=document.getElementById("name");

        if(!name||name.value==null)

            alert("Please input the name first");

        else

        {

           makeRequest("MyHandler2.ashx?name="+name.value)

        }

    }

    </script>

</head>

<body>

    <form id="form1">

    <div>

        <label for="name">Name:</label>

        <input type="text" id="name" value="" />

        <input type="button" id="button" value="Request" onclick="myRequest()"/>       

    </div>

    </form>

</body>

</html>

 

 

2. 新建一个Generic Handler文件,不妨起名为MyHandler.ashx,加入如下代码,用以处理请求:

<%@ WebHandler Language="C#" Class="MyHandler2" %>

 

 

using System;

using System.Web;

 

 

public class MyHandler2 : IHttpHandler

{

   

    public void ProcessRequest (HttpContext context)

    {

        string name = context.Request["name"];

        if (String.IsNullOrEmpty(name))

            name = "Can't get the param";

               

        context.Response.ContentType = "text/plain";

        context.Response.Write("Hello,"+name);

    }

 

    public bool IsReusable

    {

        get

        {

            return false;

        }

    }

}

3.用以处理请求不仅可以是上面的*.ashx文件,*.aspx,*.html文件也是可以的.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值