ajax 获取服务器时间实例
2009-08-25 14:53
344 查看
ajxatest.php:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="ajxatest.js">
</script>
<style type="text/css">
.displaybox{
width:150px;
background-color:#ffffff;
border:2px solid #000000;
padding:10px;
font:24px normal verdana,helvetica,arial,sans-serif;
}
</style>
</head>
<body bgcolor="#cccccc" onload="getServerTime()">
<h1>ajax demon</h1>
<h2>get the server time without page refresh</h2>
<form >
<input type="button" value="get server time" onclick="getServerTime()"></input>
</form>
<div class="displaybox" id="showtime"></div>
</body>
</html>
ajxatest.js:
function getXMLHTTPRequest(){
var req = null;
try{
req = new XMLHttpRequest(); //e.g firefox
}
catch(err1){
try{
req = new ActiveXObject("Msxml2.XMlHTTP"); // some versions IE
}
catch(err2){
try{
req = new ActiveXObject("Microsoft.XMlHTTP"); // some versions IE
}
catch(err3){
req = false;
}
}
}
return req;
}
var http = getXMLHTTPRequest();
function getServerTime(){
var myurl = 'http://127.0.0.1/js/getdate.php';
myRand = parseInt(Math.random()*99999999999999999);
var modurl = myurl + "?rand=" + myRand;
//document.write(modurl);
http.open("GET",modurl,true);
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">';
}
}
}
getdate.php:
<?php
header('Content-Type: text/xml');
echo "<?xml version=\"1.0\"?><timenow>" . date('H:i:s')."</timenow>";
?>
里面用到一个图片,附件中有
这个例子非常简单,但很实用,对于初学者学习ajax有相当的意义。可以通过个例子了解ajax的工作原理。本文出自 “好事成双” 博客,请务必保留此出处http://daven.blog.51cto.com/18445/195641
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="ajxatest.js">
</script>
<style type="text/css">
.displaybox{
width:150px;
background-color:#ffffff;
border:2px solid #000000;
padding:10px;
font:24px normal verdana,helvetica,arial,sans-serif;
}
</style>
</head>
<body bgcolor="#cccccc" onload="getServerTime()">
<h1>ajax demon</h1>
<h2>get the server time without page refresh</h2>
<form >
<input type="button" value="get server time" onclick="getServerTime()"></input>
</form>
<div class="displaybox" id="showtime"></div>
</body>
</html>
ajxatest.js:
function getXMLHTTPRequest(){
var req = null;
try{
req = new XMLHttpRequest(); //e.g firefox
}
catch(err1){
try{
req = new ActiveXObject("Msxml2.XMlHTTP"); // some versions IE
}
catch(err2){
try{
req = new ActiveXObject("Microsoft.XMlHTTP"); // some versions IE
}
catch(err3){
req = false;
}
}
}
return req;
}
var http = getXMLHTTPRequest();
function getServerTime(){
var myurl = 'http://127.0.0.1/js/getdate.php';
myRand = parseInt(Math.random()*99999999999999999);
var modurl = myurl + "?rand=" + myRand;
//document.write(modurl);
http.open("GET",modurl,true);
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">';
}
}
}
getdate.php:
<?php
header('Content-Type: text/xml');
echo "<?xml version=\"1.0\"?><timenow>" . date('H:i:s')."</timenow>";
?>
里面用到一个图片,附件中有
这个例子非常简单,但很实用,对于初学者学习ajax有相当的意义。可以通过个例子了解ajax的工作原理。本文出自 “好事成双” 博客,请务必保留此出处http://daven.blog.51cto.com/18445/195641
相关文章推荐
- AJAX获取服务器当前时间及时间格式输出处理
- jquery封装的ajax方法获取web服务器时间
- Ajax获取PHP服务器上的 Json数据以及POST的综合实例
- ajax获取服务器当前时间
- Ajax获取PHP服务器上的 Json数据以及POST的综合实例
- AJAX获取服务器当前时间
- ajax获取服务器时间
- ajax获取服务器时间
- Datatables插件1.10.15版本服务器处理模式ajax获取分页数据实例解析
- Socket入门-获取服务器时间实例
- AJAX获取服务器当前时间
- 通过原生js的ajax或jquery的ajax获取服务器的时间
- JavaScript获取系统时间以及通过ajax获取服务器时间
- 用Ajax实现获取服务器上的时间的简单的例子
- ajax获取服务器当前时间
- ajax 获取服务器时间
- JS实现Ajax---例:获取服务器时间
- Ajax 获取服务器时间
- 根据ajax从服务器获取数据的时间和ID,根据最近的时间,显示一个记录重复ID的数字