I am starting to learn about ajax with jquery and I've tried a lot of googling and I can't get this test code to work can you tell me what am I doing wrong? What happens is that the ajax won't create the table. Here is my code:

我开始用jquery学习ajax,我已经尝试了很多谷歌搜索,我无法让这个测试代码工作,你能告诉我我做错了什么吗?会发生什么是ajax不会创建表。这是我的代码:

The JSP:

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <script type="text/javascript" src="js/jquery.js"></script>
    <title>My First Web App</title>
    <script type="text/javascript">
        $(document).ready(function{
            $.ajax({
                type: "GET",
                url: "users",
                dataType: "xml",
                success: function(xml){
                    $("#content").append("<table>");
                    $(xml).find("user").each(function(){
                        var firstName = $(this).find("firstName").text();
                        var lastName = $(this).find("lastName").text();
                        var password = $(this).find("password").text();
                        var email = $(this).find("email").text();
                        $("#content").append("<tr>");
                        $("#content").append("<td>" + firstName + "</td>");
                        $("#content").append("<td>" + lastName + "</td>");
                        $("#content").append("<td>" + password + "</td>");
                        $("#content").append("<td>" + email + "</td>");
                        $("#content").append("</tr>");
                    });
                    $("#content").append("</table>");
                }
            });
        });
    </script>
</head>
<body>
    <div id="content"></div>
</body>

The servlet:

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

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

/**
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
 */
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setContentType("text/xml;charset=UTF-8");
    usuarioDAO uDAO = new usuarioDAO();
    response.getWriter().write(uDAO.getAllUsers());
}

usuarioDAO:

public String getAllUsers()
{
    String xml = "";
    xml += "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
    xml += "<users>";
    try
    {
        getAllUsers = con.prepareStatement("SELECT * FROM users");
        synchronized(getAllUsers)
        {
            ResultSet res = getAllUsers.executeQuery();
            while (res.next())
            {
                xml += "<user>";
                xml += "<firstName>" + res.getString("firstName") + "</firstName>";
                xml += "<lastName>" + res.getString("lastName") + "</lastName>";
                xml += "<password>" + res.getString("password") + "</password>";
                xml += "<email>" + res.getString("email") + "</email>";
                xml += "</user>";
            }
        }
        getAllUsers.close();
    }
    catch (Exception ex)
    {
        System.out.println(ex);
    }
    xml += "</users>";
    return xml;
}

And that's about it, can you please tell me what am I doing wrong?

这就是它,你能告诉我我做错了什么吗?

1 个解决方案

#1


0

Same problem as here:

与此处相同的问题:

Using .after() to add html closing and open tags

使用.after()添加html结束和打开标签

You are trying to add table tag as pure text, but it is not working. Instead of that, add a full table (also tr) tag to the page first, then add additional elements into it. Full elements, not just partials!

您正在尝试将表标记添加为纯文本,但它无法正常工作。而不是那样,首先在页面中添加一个完整的表(也是tr)标签,然后在其中添加其他元素。完整的元素,而不仅仅是局部的!

UPDATED

Something like this:

像这样的东西:

                $("#content").append("<table id='table'></table>");
                var i = 0;
                $("#existingElement").html(content);
                $("#existingElement").find("user").each(function(){
                    var firstName = $(this).find("firstName").text();
                    var lastName = $(this).find("lastName").text();
                    var password = $(this).find("password").text();
                    var email = $(this).find("email").text();
                    var rowid = "row"+id;
                    $("#table").append("<tr id='"+rowid+"'></tr>");
                    $("#"+rowid).append("<td>" + firstName + "</td>");
                    $("#"+rowid).append("<td>" + lastName + "</td>");
                    $("#"+rowid).append("<td>" + password + "</td>");
                    $("#"+rowid).append("<td>" + email + "</td>");
                });

更多相关文章

  1. javascript实现拖动层效果代码(许愿墙)
  2. 牛客网Java刷题知识点之同步方法和同步代码块的区别(用synchroniz
  3. 在java自动生成hashCode代码问题? 请大神赐教
  4. 如何修改代码,使其仅打印单元格E和F?
  5. dom4j-java-如何获取root中具有特定元素名称的所有元素(父元素或
  6. Java常量表达式相关的编译优化代码
  7. java代码获知该方法被哪个类、哪个方法、在哪一行调用
  8. Selenium InternetExplorerDriver无法找到明显的元素。
  9. 我的构建可以规定我的代码覆盖范围永远不会变得更糟吗?

随机推荐

  1. 对c语言的认识和想法是什么
  2. c语言console.WriteLine什么意思?
  3. c语言中要求对变量作强制定义的主要理由
  4. c语言大小写字母怎么转化?
  5. dev c++怎么用
  6. c语言中do while语句怎么使用
  7. c语言取余符号是什么
  8. C语言中system()函数怎么用?
  9. c语言strlen函数用法是什么
  10. c语言%什么意思