【问题标题】:$.each is iterating through each character in array$.each 正在遍历数组中的每个字符
【发布时间】: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


【解决方案1】:

我遇到了同样的问题。我来自 servlet 的 JSON 响应是简单的数组: [“一二”] 在使用 jquery ajax 进行迭代时,它会遍历每个字符而不是整个元素,即使在 servlet 端,响应内容类型设置为“application/json”。然后我在ajax调用属性中添加了dataType:'json',它起作用了。

$.ajax({
    type:'POST',
    data: {
        moduleName: moduleName,
        action: 'getSubModules'
    },
    dataType: 'json', //this one
    url:'AjaxController',
    success: function(response){
        $.each(response, function(index, value) {
            alert('value');
        });
    }
});

我认为它之前将响应视为纯字符串。

【讨论】:

    【解决方案2】:

    我尝试了代码,它与您提供的数据按预期工作。我建议在你的函数中使用 console.log 来查看你的数组中发生了什么。

    > x=    ({"photosets":{"photo set":[{"id":"123456789", "primary":"102932423",...
    > xx=[]
    > $.each(x.photosets.photoset,function(i,photoinfo){ xx.push(photoinfo.id); });
    > xx
    ["123456789", "012345678"]
    > $.each(xx,function(index,value){ console.log(value); });
    123456789
    012345678
    

    【讨论】:

    • 刚刚尝试过,似乎每个字符都被拆分了
    • 绝对不会那样做的。
    • 我猜那 setInfo += '' 有错吗?
    • @romanish 我虽然是这样..但我现在收到一个错误(粘贴在上面)。在stackoverflow上使用firebug控制台时,我得到了一个很好的数组,但是在本地加载实际页面时,我得到了上面的jQuery错误。我试图切换到 stackoverflow 的相同版本的 jQuery,但没有运气。
    • @romanish,你提醒我在混合中抛出 console.log() 帮助我调试并最终找出我的错误。我最初想运行此脚本并将生成的数组放入我的 html 文件中,以供另一个 ajax 调用以供参考。我的错误包括: $('#flickrSets').html(setInfo);删除该行解决了这个问题。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2023-04-07
    • 1970-01-01
    • 1970-01-01
    • 2015-03-03
    • 2018-02-19
    • 2015-09-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多