zoukankan      html  css  js  c++  java
  • 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程序返回的值操作结束

  • 相关阅读:
    MySQL的语句执行顺序
    linux 常用命令
    scala 样例类转json字符串
    Hadoop之——HDFS的RPC机制
    Hadoop之——机架感知配置
    hadoop-2.6.0-cdh5.14.0 集群高可用搭建
    spark 运行在YARN上参数配置
    日志框架SLF4J和log4j以及logback的联系和区别
    spark的rdd.saveastextfile可以追加写入hdfs同一个文件吗?
    ojdbc14-10.2.0.1.0 jar包下载
  • 原文地址:https://www.cnblogs.com/ajun/p/2662475.html
Copyright © 2011-2022 走看看