jQuery.ajax 为什么不从 php json_encode 访问该项目


jQuery.ajax Why not access the item from php json_encode?

我不明白,因为当数据输入登录名时是正确的,我做出了这个比较响应.success == "成功" 什么也没发生,我用火虫检查,结果是这样的:

响应

[{"ncontrol":"09680040","nombre":"Edgardo","apellidop":"Ramirez","apellidom":"Leon","tUser":"Admin","status":"success"}]

jQuery.ajax 脚本来发送数据

    // jQuery.ajax script to send json data to php script
    var action = $("#formLogin").attr('action');
    var login_data = {
        ncontrol: $("#ncontrolLogin").val(),
        password: $("#passwdLogin").val(),
        is_ajax: 1
    };  
    $.ajax({
        type: "POST",
        url: action,
        data: login_data,
        dataType: "json",
        success: function(response)
        {   
            **if(response.status == "success")** {
                $("#status_msg").html("(+) Correct Login");
            }
            else {
                $("#status_msg").html("(X) Error Login!");
            }
        }
    }); 
    return false;   

并且是用于处理来自jQuery.ajax的变量的PHP脚本

$ncontrolForm = $_REQUEST['ncontrol'];
$passForm = $_REQUEST['password'];
$jsonResult = array();
    $query = "SELECT * FROM users WHERE ncontrol = '$ncontrolForm' AND cve_user = SHA('$passForm')";
    $result = mysqli_query($con, $query) or die (mysqli_error());
    $num_row = mysqli_num_rows($result);
    $row = mysqli_fetch_array($result);
    if( $num_row >= 1 ) {       
        $_SESSION['n_control'] = $row['ncontrol'];
        $_SESSION['t_user'] = $row['tUser'];
        $jsonResult[] = array (
            'ncontrol' => $row['ncontrol'],
            'nombre' => $row['nombre'],
            'apellidop' => $row['apellidop'],
            'apellidom' => $row['apellidom'],               
            'tUser' => $row['tUser'],
            'status' => 'success',
        );
        header('Content-Type: application/json');
        echo json_encode($jsonResult);
    }

你有一个数组,所以这样做:

if(response[0].status == "success") {

对象是数组中的第一项。

编辑:

更仔细地观察您的 PHP,看起来您可能打算遍历查询响应中的几行并将它们添加到您的$jsonResult中。我没看错吗?