簡體   English   中英

從servlet獲取json響應並顯示在jsp表中?

[英]Getting json response from servlet and display in jsp table?

我正在調用servlet以從數據庫中獲取對象列表,並且該列表以json的形式從servlet返回。 相同的json響應將顯示在jsp表中,如下所示。

Servlet代碼:

        String json = new Gson().toJson(resultList); 
        response.setContentType("application/json"); //here i have the data and i came to know by debugging 
        response.setCharacterEncoding("UTF-8");
        response.getWriter().write(json);

jQuery代碼:

$.ajax({
    type: "GET",
    url: "./dataFetchController",
    success: function(responseJson) {

console.log(responseJson); //it is not printing any result on firebug console

        var master = $(this).parents("table.myTable");

        $.each(responseJson, function(index, contact) {    // Iterate over the JSON array.
            // Get a new row based on the prototype row
            var prot = master.find(".prototype").clone();
            prot.attr("class", "");
            prot.find("#myName").attr("value", contact.name);
            prot.find("#myLastName").attr("value", contact.lastName);

            //master.find("tbody").append(prot);
            jQuery('table.myTable tr:last').before(prot);
        });
    },
    error: function(ob,errStr) {
        $('#contactForm #formProgress').html('');
        $('#contactForm #formProgress').html('<img src="./public/images/error.png" /> <span style="color:red">Save is not successful. Try Again.</span>');
    }
});

我沒有得到任何json結果。 我嘗試使用控制台打印它,但沒有結果,但是在servlet中,它具有從數據庫返回的數據並使用調試進行了驗證。 我在這里想念什么嗎?

謝謝!

我在您的Ajax請求中看不到“ dataType”,例如:

dataType:'json'

嘗試添加它。

因此,它將變為:

$.ajax({
 type: "GET",
 url: "./dataFetchController",
 success: function(responseJson) {
       //alert and success handler
 },
 dataType: 'json',
 error : function(){
   // error handler
 });

dataType是您期望從服務器返回的數據類型

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM