麻烦的getJSON来显示数组值 [英] Trouble getting getJSON to display values from array
问题描述
我无法显示 $。的getJSON
通话的内容。我有这个code从页面获取一些JSON数据。
I am having trouble displaying the contents of a $.getJSON
call. I have this code which retrieves some JSON data from a page.
var loadItems = function() {
if (hasNextPage === false) {
return false
}
pageNum = pageNum + 1;
var url = baseUrl + "json/" + pageNum + '/';
var jqxhr = $.getJSON(url, function (data) {
var a = [];
$.each(data.itemList, function (item) {
a.push("<li>" + item.title + "</li>");
});
$("<ul/>", {html: a.join(" ")}).appendTo("#anchor");
});
jqxhr.complete(function () { alert('done!'); $(window).bind('scroll', loadOnScroll); });
};
的想法是刚刚加载页面基础上动态滚动位置,并获得该网页的JSON内容(无限滚动效果)。 JSON的结构是这样的:
The idea is just to load a page dynamically based on scroll position, and get the JSON contents of that page (for an infinite scroll effect). The JSON structure is something like this:
{ "key1": val1, "key2": val2, "itemList": [ {"title": "title", "author": "author", "id": 100, "slug": slug, }, ... ] }
最重要的价值是ITEMLIST,在那里我有检索,将获得插入我的Django的模板,这反过来会得到一些数据从视图中的数据。似乎一切都工作得很好,但我不能访问数据ITEMLIST。似乎没有任何将被推入阵。因为没有被显示在页面上(即&LT;李&GT;
应创建)。这似乎是一个简单的实现了基本的AJAX的举动,但它不工作。
The important values are in itemList, where I have to retrieve data that will get plugged into my django template, which in turn will get some data from the view. Everything seems to work just fine, except that I can't access the data in itemList. Nothing seems to get pushed into the array. because nothing gets displayed on the page (namely, the <li>
that should be created). This seems to be a simple implementation of a basic ajax move, but its not working.
编辑:
我已经做了一些bug跟踪并发现了code从不执行$。每个循环。不知道如何解决这个虽然。
I've done some bug tracking and have found that the code never executes the $.each loop. Not sure how to resolve this though.
推荐答案
您应该使用错误日志,检查了什么错误:总是使用索引,OBJ格式 $每
是安全的。
you should be using error log to check what is going wrong:and always use index,obj format in $.each
to be safe.
$.getJSON(url).success( function( data ) {
console.log(data);
$.each(data.itemList, function (index, item) {
a.push("<li>" + item.title + "</li>");
});
$("<ul/>", {html: a.join(" ")}).appendTo("#anchor");
}).error(function(error){
console.log(error);// see if you are getting in error log..
});
和你的JSON数据也是错误的,应该是这样的:
and your json data is also wrong it should be something like this:
{ "key1": "val1",
"key2": "val2",
"itemList":
[
{
"title": "title", "author": "author", "id": 100, "slug": "slug"
},.......
]
}
尝试与修正后的数据,将工作。 你可以在这里检查您的数据: http://json.parser.online.fr/
try with corrected data it will work. you can check your data here:http://json.parser.online.fr/
这篇关于麻烦的getJSON来显示数组值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!