功能:单击Get Time显示服务器时间
代码如下://time.php
echo json_encode(microtime(true));
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<script src="json.js" type="text/javascript"></script>
<script type="text/javascript">
function getXHR(){
var req;
if(window.XMLHttpRequest) {
req = new XMLHttpRequest();
} else if (window.ActiveXObject) {
req = new ActiveXObject("Microsoft.XMLHTTP");
} else {
alert('The browser does not support AJAX!');
} return req;
} function getTime() {
xhr = getXHR(); rand = Math.random(1);
url = "http://localhost/example/time.php?id=" + rand; xhr.open("GET", url, true); xhr.send(); json = xhr.responseText; time = json.parseJSON(); alert(time); div = document.getElementById("jsonData"); div.innerHTML = time;
}
</script>
</head>
<body>
<a href="#" onclick="getTime()">Get Time</a>
<div id="jsonData"></div>
</body>
</html>firebug提示:JSON.parse return JSON.parse(this, filter);