0

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.

1 Answer 1

1

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)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.