日期:2014-05-16  浏览次数:20573 次

初学AJAX,基础问题 - Web 开发 / Ajax
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=gb2312" />
<title>无标题文档</title>
<script language="javascript">
    var currentPage = 1;
    var xmlhttp = null;
    function getXmlhttp() {
        return new ActiveXObject("microsoft.xmlhttp");
    }
    
    function previous() {
        currentPage --;
        setTimeout("getData()","1000");
        
    }
    
    function next() {
        currentPage ++;
        setTimeout("getData()","1000");
    }
    
    function getData() {
    
    if (xmlhttp == null) {   ////这里,如果我加上IF,程序只能正确调用这个getData方法一次,以后再调用这个方法就拿不到数据!如果把IF块去掉,就行了,这是为什么?
        xmlhttp = getXmlhttp();
        xmlhttp.onreadyStateChange = readystate;
    }
        xmlhttp.open("get","http://b9426f32195d423:8080/ajax/servlet/ajax?page="+currentPage+"&math="+Math.random(),true);
        xmlhttp.send(null);
    }
    
    function readystate() {
        if (xmlhttp != null) {
            if (xmlhttp.readystate == 4) {
                if (xmlhttp.status == 200) {
                    var table = document.all["table"];
                    
                    var tbody = table.tBodies[1];
                    var xml = xmlhttp.responseXml;
                    var arr = xml.getElementsByTagName("note");
                    if (arr.length == 0) {
                        alert("没有数据!");
                        return;
                    }
                    
                    var length = tbody.rows.length;
            
                    for (var ac = length;ac>0;ac--) {
                        tbody.deleteRow(tbody.rows[ac]);
                    }
                    table.setAttribute("border","1");
                    for (var i = 0;i<arr.length;i++) {
                        tbody.insertRow(i);
                        var node = arr[i];
                        var chi = node.childNodes;
                        for (var a = 0;a<chi.length;a++) {
                            tbody.rows[i].insertCell(a);
                            tbody.rows[i].cells[a].appendChild(document.createTextNode(chi[a].text));
                        }
                    }

                    document.body.removeChild(document.getElementById("dddd")); ////还有这里,为什么这个层删不掉呢?它一直显示在网页上!
                    alert("done");
                } else {
                    alert("读取数据出错!");
                }
            } else if (xmlhttp.readystate == 1) {
                var div = document.createElement("div");
                div.style.borderStyle = "solid";
                div.style.borderColor = "#333333";
                div.style.borderWidth = "2px";
                div.style.backgroundColor = "blue";
                div.style.fontWeight = "bold";
                div.style.fontSize = "14px";
                div.style.width = "200px";
                div.style.height = "25px";
                div.style.textAlign = "center";
                div.style.color = "yellow";
                div.innerHTML = "正在读取.....";
                div.style.position = "absolute";
                div.style.left = (parseInt(document.body.clientWidth) - 200) + "px";
                div.style.top = "0px";
                div.setAttribute("id","dddd");
                document.bod