【问题标题】:Parsing json returns only one object in django jquery解析json在django jquery中只返回一个对象
【发布时间】:2016-08-14 18:10:02
【问题描述】:

朋友们,我正在尝试在 django 中获取 ajax json 响应。

虽然控制台日志显示每个 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); 
   }
  });
}

我不知道我哪里出错了。

【问题讨论】:

    标签: jquery json django


    【解决方案1】:

    问题出在这一行:

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

    每次刷新 html 时,只传递一个值给它。尝试将内容附加到 html 文件中,或创建多个实例(作为列表 ro 的东西)

    【讨论】:

      猜你喜欢
      • 2014-10-17
      • 2014-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-31
      相关资源
      最近更新 更多