<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文件的代码:
<?php header('Content-Type: text/xml'); echo "<?xml version=\"1.0\" ?><clock1><timenow>" .date('H:i:s')."</timenow></clock1>"; ?>
1 个解决方案
#1
0
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/.
可能是很多事情,因为正如其他人指出我们想知道你所看到的。但有一种猜测是,您请求的URL不与html文件托管在同一服务器上。也就是说,你是一个html页面应该在http:// localhost /上。
#1
0
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/.
可能是很多事情,因为正如其他人指出我们想知道你所看到的。但有一种猜测是,您请求的URL不与html文件托管在同一服务器上。也就是说,你是一个html页面应该在http:// localhost /上。