2016-08-14 101 views
0

朋友,我試圖在django中獲取ajax json響應。解析json在django中只返回一個對象jquery

雖然控制檯日誌顯示了每個json對象,但在解析json並向html中的分區添加字段時,情況並非如此。只有一個對象的字段出現。

我的JavaScript如下

$("#all_questions").on("click", 
    function(event){ 
     all_questions(); 
    } 
); 

function all_questions() 
{ 
    console.log("all_questions() working.."); 


    $.ajax({ 
     url : "/questions", 
     type : "GET", // http method 
     data : { }, 

     success : function(json) { 
     console.log(json); // log the returned json to the console 
     $("#content").empty(); 
     data = JSON.parse(json); 
     $.each(data, function(idx, obj) { 
      $('#content').html(obj.fields.title); 
     }); 
    } 
    , 


    error : function(xhr,errmsg,err) { 

     console.log(xhr.status + ": " + xhr.responseText); 
    } 
    }); 
} 

我想不通我要去哪裏錯了。

回答

1

問題就出在這行:

$('#content').html(obj.fields.title);

刷新HTML每一次,你只有一個值傳遞給它。嘗試追加內容在一個html文件,或創建多個實例(作爲一個列表ro的東西)

+0

感謝patito,它幫助! – amankarn