学习AJAX,HTML代码不会做任何事情

时间:2012-03-11 03:47:47

标签: javascript html ajax

我已经设置了一个servlet,显示我写的checkersgame的用户名,密码,胜利和损失。我想创建一个ajax程序来访问这个页面,并创建一个列出用户胜负的表(就像一个简单的排行榜)。但是,我的HTML代码无法正常工作,我无法理解为什么。我可以登录到我列为变量url的页面,并从servlet中查看xml文件。任何帮助是极大的赞赏。

<html>
<head>
<script type="text/javascript">
function requestUpdate()
{
    var req = newXMLHttpRequest();
    req.onreadystatechange = function()
    {   
        if(req.readyState == 4)
        {
            displayResult(req);
        }
    }
    var url = "http://localhost:8080/Checkers/CheckersServlet"; 
    req.open("GET",url,true);
    req.send(null);
}

function displayResult(req)
{
    var div = document.getElementById("Scoreboard");
    div.innerHTML = "<table border> <tr> <th> Username </th> <th> Wins </th> <th> Losses </th> </tr>" ;

    var doc = req.responseXML;
    var Users = doc.getElementsByTagName('Username');
    var Wins = doc.getElementsByTagName('Wins');
    var Losses = doc.getElementsByTagName('Losses');
    var U;
    var W;
    var L;
    for(var i = 0; i < Users.length; i++)
    {
        U = Users.item(i).textContent;
        W = Wins.item(i).textContent;
        L = Losses.item(i).textContent;

        div.innerHTML = div.innerHTML + "<tr> <td>" + U + "</td> <td>" + W + "</td> <td>" + L + "</td> </tr>";
    }

div.innerHTML = div.innerHTML + "</table>";
}
</script>
</head>
<body onload = "requestUpdate()">
    <button onclick = "requestUpdate()">Update</button>
    <div id = "Scoreboard"></div>
</body>

这是servlet代码

import java.io.IOException;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class CheckersServlet
 */
@WebServlet("/CheckersServlet")
public class CheckersServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;


/**
 * @see HttpServlet#HttpServlet()
 */
public CheckersServlet() {
    super();
    // TODO Auto-generated constructor stub
}

/**
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
 */
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
{
    // TODO Auto-generated method stub
    response.setContentType("text/xml");
    PrintWriter out = response.getWriter();
    out.println("<?xml version='1.0'?>" );

    BufferedReader fReader = new BufferedReader(new FileReader("/Users/adamsturge991/desktop/checkersdatabase.txt"));
    String data = fReader.readLine();
    out.println("<CheckersData>");
    while(data != null)
    {
        out.println("<User>");
        out.println("<Username>");
        out.println(data); 
        out.println("</Username>");
        data = fReader.readLine();
        out.println("<Password>");
        out.println(data);
        out.println("</Password>");
        data = fReader.readLine();
        out.println("<Wins>");
        out.println(data);
        out.println("</Wins>");
        data = fReader.readLine();
        out.println("<Losses>");
        out.println(data);
        out.println("</Losses>");
        out.println("</User>");
        data = fReader.readLine(); //get next username or null if end of database
    }
    out.println("</CheckersData>");
}

/**
 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
 */
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
}

}

2 个答案:

答案 0 :(得分:3)

这一行:

var req = newXMLHttpRequest();

new之后需要一个空格,所以:

var req = new XMLHttpRequest();

(另请注意,在div.innerHTML函数中不断更新displayResult()将无法提供良好的性能。最好在字符串变量中构建所需的html,然后将div.innerHTML设置为等于变量在最后。)

答案 1 :(得分:2)

这一行:

var req = newXMLHttpRequest(); 

应该是:

var req = new XMLHttpRequest();

这一行:

var doc = req.responseXML;

应该是:

 var doc = req.responseText;

更新

var doc = req.responseText;

/*This is for making XML doc, otherwise `getElementsByTagName` wont work*/
if (window.DOMParser)
{
   parser=new DOMParser();
   xmlDoc=parser.parseFromString(doc,"text/xml");
}
else // Internet Explorer
{
  xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
  xmlDoc.async=false;
  xmlDoc.loadXML(doc);
} 

/*XML DOC is ready, now parse it with `getElementsByTagName1`*/ 
var Users = xmlDoc.getElementsByTagName('Username');
var Wins = xmlDoc.getElementsByTagName('Wins');
var Losses = xmlDoc.getElementsByTagName('Losses');