ajax XMLHttpRequest请求页面返回的值和ashx处理程序返回的值

首先创建一个页面ajaxTest.aspx用来获取服务器时间,

前端代码

<head runat="server">
    <title>无标题页</title>

     <script type="text/javascript">
      var xmlHttp; //定义xmlHttpRequest变量

     //创建XMLHttpRequest对象
      function CreateXmlHttp()
      {         
          if(window.XMLHttpRequest)
          {
             //用户当前所使用的浏览器是IE7以上版本,或非IE浏览器
             xmlHttp = new XMLHttpRequest();
          }
         
          if(window.ActiveXObject)
          {
             try
             {
                 //说明用户所使用的浏览器是IE5或IE6
                 xmlHttp = new ActiveXObject("msxml2.XMLHTTP");
              
             }
             catch(e)
             {
                try
                {
                     //说明用户所使用的浏览器是IE4即以下版本
                     xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
                }
                catch(ex) 
                {}             
             }
          }
      }    

     //注册XMLHttpRequest服务
      function registerServer()
      {
          url = "ajaxresponse.aspx?callback=true"; // 操作其它页面返回的数据
          url = "ajaxHandler.ashx?name="+ encodeURIComponent("小明")+"&age=25";  //操作ashx返回的数据
          CreateXmlHttp(); //创建XMLHttpRequest
          xmlHttp.open("Post",url,true);      
          xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
          xmlHttp.onreadystatechange = callBack;
          xmlHttp.send(null);
      }
      function callBack()
      {
          if(xmlHttp.readyState == 4)
          {            
                 //数据加载完成
                 document.getElementById("username").value = xmlHttp.responseText;          
          }
          else
          {
                 document.getElementById("username").value = "数据加载中.....";
          }                   
      }
     
      window.onload = function(){
           registerServer();       
      }
    </script>

</head>

<body>
    <form id="form1" runat="server">
    <div>
        <input type="text" id="username" />
    </div>
    </form>
</body>

 创建另一个页面ajaxresponse.aspx返回服务起时间,后台代码如下

  if ((Request.QueryString["callback"] ?? "") == "true")
        {
            Response.Write("时间:" + DateTime.Now.ToString());
            Response.Flush();
            Response.End();
        }

创建ajaxHandler.ashx返回字符串

public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        //context.Response.Write("Hello World");
        string name = HttpContext.Current.Request.QueryString["name"] ?? "";
        string age = HttpContext.Current.Request.QueryString["age"] ?? "";
        context.Response.Write(GetString(name, age));
    }
    public string GetString(string name, string age)
    {
        string str = name + "今年" + age + "岁了";
        return str;
    }
    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

 

到此ajax处理页面和ashx程序返回的值操作结束

posted @ 2012-08-29 17:50  ajunfly  阅读(2736)  评论(0编辑  收藏  举报