【发布时间】:2011-08-07 02:20:42
【问题描述】:
我通过从 json ajax 请求中提取数据来创建一个数组。但是在另一个 $.each 语句中使用该数组会导致迭代每个字符而不是每个值。
var setInfo = [];
$.ajax({
url: blah,
async: false,
dataType: 'json',
success: function{
$.each(data.photosets.photoset, function(i,photoinfo) {
setInfo.push(photoinfo.id);
}) // completed set values look like 1234,5678,etc..
holler();
})
function holler() {
$.each(setInfo, function(index, value) {
// using each array value in here splits into each
// character rather than each value. So rather than
// iterating through each whole value 1234,5678,etc..
// It is iterating as 1,2,3,4,5,6,7,8,and so on.
var url1 = "http://www.test.com/" + value;
// Rather than http://www.test.com/1234, value is being evaluated as just 1
// so the result is http://www.test.com/1
});
这是我获取 id 的 json 响应示例:
({"photosets":{"photoset":
[{"id":"123456789", "primary":"102932423", "secret":"19ca84349a", "server":"5143", "farm":6, "photos":"52", "videos":0, "title":{"_content":"Thanksgiving 2010"}, "description":{"_content":""}},
{"id":"012345678", "primary":"1294872352", "secret":"983a9c58d1", "server":"5184", "farm":6, "photos":"12", "videos":0, "title":{"_content":"24th Birthday Dinner at McCormick and Schmitts"}, "description":{"_content":""}}]}, "stat":"ok"})
编辑:
看来我有一段代码,我假设它正在将数组转换为字符串:
setInfo += '';
在 stackoverflow 上的 firebug 上的控制台中运行我的代码,我可以看到一个数组现在显示为 [1234,5678] 但在本地运行,我收到一个奇怪的 jQuery 错误:
uncaught exception: [Exception... "Could not convert JavaScript argument arg 0 [nsIDOMDocumentFragment.appendChild]" nsresult: "0x80570009 (NS_ERROR_XPC_BAD_CONVERT_JS)" location: "JS frame :: http://code.jquery.com/jquery-1.5.js :: <TOP_LEVEL> :: line 5579" data: no]
Line 0
【问题讨论】:
标签: jquery ajax arrays loops each