jQuery / Ajax:如何将数组作为 Ajax 成功函数的一部分进行循环

Tan*_*uta 3 arrays ajax jquery loops

我有一个Ajax 调用,它返回一个数组,需要对这个数组中的每个值做一些事情。

到目前为止,我有以下内容,但这会返回以下错误:

Uncaught TypeError: Cannot use 'in' operator to search for 'length' in array(5)...
Run Code Online (Sandbox Code Playgroud)

有人可以告诉我我在这里做错了什么。我该如何处理每个值?

我的阿贾克斯:

$.ajax({        
    type: "post",   
    url: "ajax.php",
    cache: "false",
    data: {
        node: 'fetchValues',
        itemIDs: itemIDs
    },
    success: function(data){
        console.log(data);  // for testing only
        jQuery.each(data, function(index, value){
            console.log(value);
        });
    }
});
Run Code Online (Sandbox Code Playgroud)

“数据”示例(来自控制台日志)

array(5) {
  [1]=>
  string(6) "Value1"
  [2]=>
  string(6) "Value2"
  [3]=>
  string(6) "Value3"
  [4]=>
  string(6) "Value4"
  [5]=>
  string(6) "Value5"
}
Run Code Online (Sandbox Code Playgroud)

非常感谢您的帮助。

But*_*fly 5

好像你的数组没有正确解析

在发送响应之前从 php 端

echo json_encode($result); // REPLACE $result WITH  YOUR OUTPUT ARRAY
Run Code Online (Sandbox Code Playgroud)

在 jquery 方面:

$.ajax({        
    type: "post",   
    url: "ajax.php",
    dataType : 'JSON',
    cache: "false",
    data: {
        node: 'fetchValues',
        itemIDs: itemIDs
    },
    success: function(data){
        console.log(data);  // for testing only
       var data=$.parseJSON(data);
        jQuery.each(data, function(index, value){
            console.log(value);
        });
    }
});
Run Code Online (Sandbox Code Playgroud)

参考:http : //api.jquery.com/jquery.parsejson/