Why am I unable to get sever time?
<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>
Code for the PHP file:
<?php header('Content-Type: text/xml'); echo "<?xml version=\"1.0\" ?><clock1><timenow>" .date('H:i:s')."</timenow></clock1>"; ?>
Could be lots of things, since as others have pointed out we'd like to know what you are seeing. But one guess is that the url you are requesting is not hosted on the same server as the html file. That is, the html page you are one should be on http://localhost/.
精彩评论