Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在jQuery中循环遍历数组_Jquery_Arrays_Loops - Fatal编程技术网

在jQuery中循环遍历数组

在jQuery中循环遍历数组,jquery,arrays,loops,Jquery,Arrays,Loops,如何在jQuery中循环使用这个$user数组?如果返回“Failure”,则应打印错误 实际上是打印出来的,但输出错误:undefinedadmin\u 1、admin\u 2、admin\u 3 提前谢谢 <?php $id = $_POST['id']; if($id == 1) { $users['data'] = array(array('name'=> 'admin_1'), array('name'=> 'admin_2'), array('name'=

如何在jQuery中循环使用这个$user数组?如果返回“Failure”,则应打印错误

实际上是打印出来的,但输出错误:undefinedadmin\u 1、admin\u 2、admin\u 3

提前谢谢

<?php
$id = $_POST['id'];

if($id == 1)
{
    $users['data'] = array(array('name'=> 'admin_1'), array('name'=> 'admin_2'), array('name'=> 'admin_2'));
    echo json_encode($users);
}
else
{
    $users['data'] = 'Failure';
    echo json_encode($users);
}
?>



$.ajax({
    type        : 'POST',
    url         : 'list.php',
    data        : 'id=' + text_id,
    dataType    : 'json',
    success     : function(response)
    {
        //IF not 'Failure', loop through the array and print content into div.success
        //IF 'Failure', show div.fail
    }
});

$.ajax({
键入:“POST”,
url:'list.php',
数据:“id=”+文本id,
数据类型:“json”,
成功:功能(响应)
{
//如果不是“失败”,则循环遍历数组并将内容打印到div.success中
//如果“失败”,则显示div.fail
}
});
如果(response.data=='Failure'){
console.log('error');
返回false;
}
对于(var i=0;i
我必须将名称存储在变量中,然后将其打印出来,但是result=result+response.data[I].name+',';按如下方式打印:undefinedadmin\u 1、admin\u 2、admin\u 3谢谢。这很有帮助。
if ($.isArray(response)) {
    //loop through array
} else {
    //show error
}
if(response.data == 'Failure') {
    console.log('error');
    return false;
}

for(var i = 0; i < response.data.length; i++) {
    if(typeof response.data[i].name != 'undefined') {
        console.log(response.data[i].name);
    }
}