amankarn amankarn - 3 months ago 11
jQuery Question

Parsing json returns only one object in django jquery

Friends, i'm trying to get ajax json response in django.

While the console log shows every json object, the same is not true while parsing the json and adding a field to a division in html. Only one object's field shows up.

my javascript is as follows

$("#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);
}
});
}


I can't figure out where i'm going wrong.

Answer

The problem lies in this line:

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

Every time you refresh html, you only pass one value to it. Try appending the content in an html file, or creating multiple instances (as a list ro something)

Comments