在AJAX请求中从PHP返回的一个变量,但另一个不是

时间:2014-04-19 17:46:23

标签: javascript php jquery ajax

我有两个由AJAX请求返回的类似变量 - countrysalt。前者正确返回,而后者为空。

但是,我知道该变量在PHP脚本中的最后else循环之前是正常的,因为salt已正确添加到此行中的哈希:$hash = hash('sha512', $password . $salt);以进行验证。

但是,当我在Javascript中提醒它时,它会返回{}

JS:

$.ajax
({
    type: "POST",
    url: "http://www.domain.com/includes/login.php",
    data: $('#login-form').serialize(),
    success: function(data)
    {
        if (data == 'Incorrect email or password')
        {
            $('#login-response').hide();
            $('#login-response').fadeIn();
            $('#login-response').html(data);
        }
        else
        {
            chrome.storage.local.set({'country': data.country});

            chrome.storage.local.get('country', function (result)
            {
                alert(JSON.stringify(result)); // returns as expected
            });

            chrome.storage.local.set({'salt': data.salt});

            chrome.storage.local.get('salt', function (result)
            {
                alert(JSON.stringify(result)); // returns simply {}
            });
        }
    },
    error: function()
    {
        alert("fail");
    }
});

PHP:

<?php

$username = $_POST['loginEmail'];
$password = $_POST['loginPassword'];

include('connect.php');
$username = mysqli_real_escape_string($con, $username);
$query = "SELECT password
        FROM sh_users
        WHERE username = '$username';";

$result = mysqli_query($con, $query);

if (mysqli_num_rows($result) == 0)
{
    echo "Incorrect email or password";
}

else
{
    $query = "SELECT *
        FROM sh_users
        WHERE username = '$username';";

    $result = mysqli_query($con, $query);

    $userData = mysqli_fetch_array($result, MYSQL_ASSOC);
    $salt = $userData['salt'];
    $country = $userData['country'];
    $gender = $userData['gender'];

    $hash = hash('sha512', $password . $salt);

    if ($hash != $userData['password'])
    {
        echo "Incorrect email or password";
    }
    else
    {
        $data['salt'] = $salt;
        $data['country'] = $country;
        echo json_encode($data, true);
    }
}
?>

0 个答案:

没有答案