简体   繁体   中英

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

I do not understand because when the data were entered into the login are correct and I make this comparison response.success == "success" nothing happens, I check with firebug and the result is this:

Response

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

jQuery.ajax script to send data

    // 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;   

And is PHP Script for processing variables from jQuery.ajax

$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);

    }

You have an array, so do it this way:

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

The object is the first item in the array.

EDIT:

Looking more closely at your PHP, it looks like you might be intending to loop through several rows in your query response and add them to your $jsonResult . Am I seeing it right?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM