zoukankan      html  css  js  c++  java
  • Ajax初识

    <!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>
        <title></title>
        <script type="text/javascript">
            function btnClick() {
                
    //                //创建一个兼容三大主流浏览器的xmlhttpRequest对象
    //                var xmlhttp = false;
    //                try{
    //                    xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");//ie msxml3.0+
    //                }
    //                catch(e){
    //                    try{
    //                        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");//ie msxml2.6
    //                    }
    //                    catch(e2){
    //                        xmlhttp = false;
    //                    }
    //                }
    //                if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {//Firefox,Opera 8.0,Safari
    //                    xmlhttp = new XMLHttpRequest();
    //                }
    //                return xmlhttp;
    //            }
                var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); //创建一个XMLHTTP对象
                
                if (!xmlhttp) {
                    alert("创建xmlhttp对象异常!");
                    return false;
                }
                xmlhttp.open("POST", "GetDate1.ashx?id="+encodeURI("中国")+"&ts" + new Date(), true); //准备向服务器的GetDate1.ashx发送请求
                
                //XMLHTTP默认(也推荐)不是同步请求的,也就是open方法并不像webClient的DownloadString那样把服务器返回的数据拿到才返回,是异步的,因此需要监听onreadystatechange事件
                xmlhttp.onreadystatechange = function () {
                    if (xmlhttp.readyState == 4) {
                        if (xmlhttp.status == 200) {//如果状态是200表示成功
                            document.getElementById("Text1").value = xmlhttp.responseText;//responseText属性为服务器返回的数据
                        }
                        else {
                            alert("Ajax服务器返回错误!");
                        }
                    }
                }
                xmlhttp.send();
            }


        </script>
    </head>
    <body>
        <input id="Text1" type="text"/>
        <input type="button" id="Button1" value="button" onclick="btnClick()"/>
    </body>

    </html>



    ashx:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;


    namespace AJax
    {
        /// <summary>
        /// GetDate1 的摘要说明
        /// </summary>
        public class GetDate1 : IHttpHandler
        {


            public void ProcessRequest(HttpContext context)
            {
                context.Response.ContentType = "text/plain";
                string id = context.Request["id"];//获取到客户端的变量
                string ts = context.Request["ts"];
                ts = "hello";
                context.Response.Write(DateTime.Now.ToString() + "---" + id+"--"+ts); //返回给客户端数据
            }


            public bool IsReusable
            {
                get
                {
                    return false;
                }
            }
        }
    }

  • 相关阅读:
    一篇文章看清楚JDK13的特性!
    【华为云技术分享】序列特征的处理方法之一:基于注意力机制方法
    【转载】PHP简单 对象(object) 与 数组(array) 的转换
    解决URL网址中遇到%2F或%5C(正反斜杠)等特殊符号导致URL重写失效出现404的问题
    hbuilder打包APP
    win7系统访问局域网中的wamp服务器
    hbuilder检测不到夜神模拟器 -- 解决办法
    hbuilder与夜神模拟器的链接
    webstorm(10.0.2)的端口号修改
    webstorm(10.0.2)设置测试服务器 -- 局域网内其他设备访问
  • 原文地址:https://www.cnblogs.com/java20130723/p/3211451.html
Copyright © 2011-2022 走看看