为什么我无法获得服务器时间?

时间:2010-11-09 16:32:20

标签: php javascript ajax

<html>
<head>
<title>Ajax Demonstration</title>
<style>
.displaybox {
width:150px;
background-color:#ffffff;
border:2px solid #000000;
padding:10px;
font:24px normal verdana, helvetica, arial, sans-serif;
}
</style>
<script language="JavaScript" type="text/javascript">
//<%=new java.util.Date()%>

function getXMLHTTPRequest() {
    alert("Hello! I am an alert box!");
    try {
        req = new XMLHttpRequest();
    } catch (err1) {
        try {
            req = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (err2) {
            try {
                req = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (err3) {
                req = false;
            }
        }
    }
    return req;
}
var http = getXMLHTTPRequest();

function getServerTime() {
    var myurl = 'http://localhost/csp 2/telltimeXML.php';
    myRand = parseInt(Math.random() * 999999999999999);
    // var modurl = myurl+"?rand="+myRand;
    http.open("GET", myurl, true);
    http.onreadystatechange = useHttpResponse;
    http.send(null);
}

function useHttpResponse() {
    if (http.readyState == 4) {
        if (http.status == 200) {
            var timeValue = http.responseXML.getElementsByTagName("timenow")[0];
            alert(timeValue);
            document.getElementById('showtime').innerHTML = timeValue.childNodes[0].nodeValue;
        }
    }
}
</script>
</head>
<body style="background-color:#cccccc"
 onLoad="getServerTime()">
<center>
<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>
</center>
</body>
</html>

PHP文件的代码:

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

1 个答案:

答案 0 :(得分:0)

可能有很多事情,因为正如其他人指出我们想知道你所看到的。但有一种猜测是,您请求的URL不与html文件托管在同一服务器上。也就是说,您所在的html页面应位于http://localhost/