AJAX只显示JSON中的第一个对象

时间:2015-06-25 09:36:42

标签: arrays json jsonp

Main.js

$(function(){ 
   $.ajax({
      type: "GET",
      url: '//localhost:8000/secure',
      dataType: "jsonp",
      success: function(data) {
        console.log(data)
      }    
   });
});

Output.json

({"posted_date":"25 Jun 2015 ","posted_ID":"3433","content":"this is content","title":"Notice 26/6"},
{"posted_date":"25 Jun 2015 ","posted_ID":"4261","content":"this is content","title":"Welcome"})

所以基本上当我运行代码时,它只显示第一个对象

{“posted_date”:“2015年6月25日”,“posted_ID”:“3433”,“内容”:“这是内容”,“标题”:“通知26/6”}

但第二个对象没有显示。

我应该怎么做才能显示BOTH对象(或整个json)?

注意:我必须使用JSONP,否则我会得到一些奇怪的错误......

2 个答案:

答案 0 :(得分:0)

x=[{"posted_date":"25 Jun 2015 ","posted_ID":"3433","content":"this is content","title":"Notice 26/6"},{"posted_date":"25 Jun 2015 ","posted_ID":"4261","content":"this is content","title":"Welcome"}]

如果您想要对象列表,那么您可以传递上面提到的内容注意&#39; [&#39;大括号代替&#39; &#39;。你正在尝试的方式是python元组方式,如果你想要那种格式,那么请检查answer。希望它有帮助< / p>

我检查了控制台,这就是我所看到的 enter image description here

答案 1 :(得分:0)

试试这个

$(function(){ 
   $.ajax({
      type: "GET",
      url: '//localhost:8000/secure',
      dataType: "jsonp",
      success: function(data) {
        var response = eval(data);
        $.each(response, function(key, event) {
        console.log(event.d)
        }
      }    
   });
});

像这样返回数据

{"d":[{"posted_date":"25 Jun 2015 ","posted_ID":"3433","content":"this is content","title":"Notice 26/6"},
{"posted_date":"25 Jun 2015 ","posted_ID":"4261","content":"this is content","title":"Welcome"}]}