jQuery - 500(内部服务器错误)

时间:2016-09-29 20:58:09

标签: javascript jquery ajax

我是jQuery和Java的新手,我正在尝试使用这两个和JSON消息构建一个简单的应用程序。我正在尝试从我的数据库中检索数据,但是当我单击按钮时没有任何反应。谷歌的工具显示错误:

GET http://localhost:8080/WebLibrary/LibraryBooks 500 (Internal Server Error) jquery-latest.min.js:4 

send @ jquery-latest.min.js:4
ajax @ jquery-latest.min.js:4
m.(anonymous function) @ jquery-latest.min.js:4
(anonymous function) @ save_book:19
dispatch @ jquery-latest.min.js:3
r.handle @ jquery-latest.min.js:3 

我已经阅读了一些类似的问题和答案,但我无法修复它。我的剧本:

<script type="text/javascript">
$(document).ready(function() {
     $("#tablediv").hide();
         $("#showBooks").click(function(event){
               $.get('LibraryBooks',function(responseJson) {
                if(responseJson!=null){
                    $("#booktable").find("tr:gt(0)").remove();
                    var table1 = $("#booktable");
                    $.each(responseJson, function(key,value) { 
                         var rowNew = $("<tr><td></td><td></td><td></td><td>");
                            rowNew.children().eq(0).text(value['title']); 
                            rowNew.children().eq(1).text(value['author_name']); 
                            rowNew.children().eq(2).text(value['author_surname']); 
                            rowNew.children().eq(3).text(value['year']); 
                            rowNew.appendTo(table1);
                    });
                    }
                });
         $("#tablediv").show();   
      });      
    });
</script>

来自LibraryBooks servlet的doGet方法

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        response.getWriter().append("Served at: ").append(request.getContextPath());

        ArrayList<BookObject> books = new ArrayList<BookObject>();
        books = FetchData.getAllBooks();
        Gson gson = new Gson();
        JsonElement element = gson.toJsonTree(books, new TypeToken<BookObject>() {}.getType());

        JsonArray jsonArray = element.getAsJsonArray();
        response.setContentType("application/json");
        response.getWriter().print(jsonArray);
    }

如果需要一些额外的代码,请告诉我。

0 个答案:

没有答案