0

我一直在关注我一直在阅读的 Ajax 书中的一个示例。

我已按照说明将代码放在一起。这个想法是获取 php 服务器时间并在单击按钮时显示它。

该出版物是 2006 年出版的,我不确定我所写的任何内容是否已过时并且可能不再受支持而导致其损坏?

如有任何指导,我将不胜感激,谢谢!

AJAX 页面

<!DOCTYPE html>
<html>
<head>
    <title>Ajax Demonstration</title>
    <style>
        .displaybox{
            width: 150px;
            margin:0 auto;
            background-color: #fff;
            border: 2px solid #000;
            padding: 10px;
            font:24px normal verdana, helvetica, arial, sans-serif;
        }
    </style>
    <script language="JavaScript" type="text/javascript">
        function getXMLHTTPRequest(){
            try{
                reg = new XMLHttpRequest();
            }
            catch(err1){
                try{
                    req = new ActiveXObject("Msxm12.XMLHTTP");
                }
                catch(err2){
                    try{
                        req = new ActiveXObject("Microsoft.XMLHTTP");
                    }
                    catch(err3){
                        req = false;
                    }
                }
            }
        }

        var http = getXMLHTTPRequest();

        function getServerTime(){
            var myurl = 'telltimeXML.php';
            myRand = parseInt(Math.random()* 999999999999999);
            //add random number to URL to avoid cache problems
            var modurl = myurl+"?rand="+myRand;
            http.open("GET", modurl, true);
            //set up the callback function
            http.onreadystatechange = useHttpResponse;
            http.send(null);
        }

        function useHttpResponse(){
            if(http.readyState == 4){
                if(http.status == 200){
                    var timeValue = http.responseXML.getElementsByTagName("timenow")[0];
                    document.getElementById('showtime').innerHTML = timeValue.childNodes[0].nodeValue;
                }
            }
            else{
                document.getElementById('showtime').innerHTML = '<img src="anim.gif">';
            }
        }
    </script>
</head>
<body style="background-color:#ccc; text-align:center" onLoad="getServerTime()">
    <h1>Ajax Demonstration</h1>
    <h2>Getting the server time without page refresh</h2>
    <form>
        <input type="button" value="Get Server Time" onClick="getServerTime()" />
    </form>
    <div id="showtime" class="displaybox"></div>
</body>

PHP 页面

<?php
header('Content-Type: text/xml');
echo "<?xml version='1.0' ?><clock1><timenow>".date('H:i:s')."</timenow></clock1>";
?>
4

1 回答 1

2

这一行:

var http = getXMLHTTPRequest();

期待getXMLHttpRequest()实例return。但是,function而是尝试直接设置req

// ...

req = new XMLHttpRequest();

// ...

可能最简单的解决方案是将每个更改req =return

function getXMLHTTPRequest(){
    try{
        return new XMLHttpRequest();
    }
    catch(err1){
        // etc.
    }
}

不过,您也可以提前声明http并直接设置它:

var http;

function getXMLHTTPRequest(){
    try{
        http = new XMLHttpRequest();
    }
    catch(err1){
        // etc.
    }
}

getXMLHttpRequest(); // no need for `http =` here, since they're in the function
于 2013-03-24T00:56:59.223 回答