Ajax实例化(五)

客户端:

<head>

    <title></title>

    <script type="text/javascript">

        function ajaxFunction() {

            var xmlHttp;

            try {

                // Firefox, Opera 8.0+, Safari

                xmlHttp = new XMLHttpRequest();

            }

            catch (e) {

                // Internet Explorer

                try {

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

                }

                catch (e) {

                    try {

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

                    }

                    catch (e) {

                        alert("您的浏览器不支持AJAX");

                        return false;

                    }

                }

            }

            xmlHttp.onreadystatechange = function () {

                if (xmlHttp.readyState == 4) {

                    document.myForm.time.value = xmlHttp.responseText;

                }

            }

            var dt = new Date();

            xmlHttp.open("GET", "AjaxTime.ashx?dt=" + dt.getSeconds(), true); //

            xmlHttp.send(null);

        }

</script>

</head>

<body>

<form name="myForm">

用户: <input type="text" name="username" onkeyup="ajaxFunction();" />

时间: <input type="text" name="time" />

</form>

</body>

</html>

服务端:

public void ProcessRequest(HttpContext context)

        {

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

            DateTime dt = DateTime.Now;           

            context.Response.Write(dt);

        }

posted @ 2011-10-12 11:55  放眼世界  阅读(224)  评论(0编辑  收藏  举报