我有一个阿贾克斯调用返回一个数组并需要对该数组中的每个值执行某些操作。
到目前为止,我有以下内容,但这会返回以下错误:
Uncaught TypeError: Cannot use 'in' operator to search for 'length' in array(5)...
有人可以告诉我我在这里做错了什么吗?我如何对每个值做一些事情?
My Ajax:
$.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);
});
}
});
“数据”的示例(来自控制台日志):
array(5) {
[1]=>
string(6) "Value1"
[2]=>
string(6) "Value2"
[3]=>
string(6) "Value3"
[4]=>
string(6) "Value4"
[5]=>
string(6) "Value5"
}
非常感谢您的帮助。
似乎你的数组没有正确解析
在发送响应之前从 php 端
echo json_encode($result); // REPLACE $result WITH YOUR OUTPUT ARRAY
在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);
});
}
});
Ref: http://api.jquery.com/jquery.parsejson/ http://api.jquery.com/jquery.parsejson/
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)