读书人

ajax的一个运用 XMLHttpRequest解决办

发布时间: 2012-04-25 19:32:32 作者: rapoo

ajax的一个运用 XMLHttpRequest
script2.html文件代码如下:

JScript code
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>Ajax Demonstration</title><style>.displaybox {    width:150px;    background-color:#FFFFFF;    border:2px solid #000000;    padding:10px;    font:24px "微软雅黑";    margin:auto;}</style><script type="text/javascript">function getXMLHTTPRequest() {    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 = "telltimeXML.php";    myRand = parseInt(Math.random() * 999999999999999);    var modurl = myurl + "?rand=" + myRand;    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">';        }    }}</script></head><body style="background-color:#CCCCCC; text-align:center"><h1>Ajax Demostration</h1><h2>Getting the server time without page refresh</h2><form><input type="button" value="Get Server Time"  /></form><div id="showtime" class="displaybox"></div></body></html>

telltimeXML.php代码如下:
PHP code
<?phpdate_default_timezone_set('PRC');echo '<?xml version="1.0" encoding="utf-8" ?><clock1><timenow>'.date("H:i:s").'</timenow></clock1>';?>


我想通过点击 Get Server Time获取时间,但是并没有实现此功能。谁帮我解答下。 还要请教大家像这种情况该如何调试?

[解决办法]
Eclipse firebug 打断点联调
[解决办法]
你把方法都定义好了可是没有看到你调用的地方啊。。 把那个按钮写成这个试试:
<input type="button" value="Get Server Time" onclick="getServerTime()" />
[解决办法]
你要弄懂ajax的原理就行了

读书人网 >JavaScript

热点推荐