【问题标题】:Access Ajax Call response PHP via jqXhr通过 jqXhr 访问 Ajax 调用响应 PHP
【发布时间】:2015-07-24 10:45:15
【问题描述】:

我在 Jquery 中使用 Ajax 调用来让用户登录网站。

JQUERY 调用如下所示

$("#login-form").validate({
    submitHandler : function(form) {
        var request;
        // bind to the submit event of our form
        // lets select and cache all the fields
        var $inputs = $(form).find("input, select, button, textarea");
        // serialize the data in the form
        var serializedData = $(form).serialize();
        // lets disable the inputs for the duration of the ajax request
        $inputs.prop("disabled", true);
        $('#submit_btn_log').html('<img src="images/loader.gif" 
        style="height:20px"/> Please Wait...');

        // fire off the request to /form.php
        request = $.ajax({
                url: "social_login/login",
                type: "post",
                data: serializedData
        });
        // callback handler that will be called on success
        request.done(function (response, textStatus, jqXHR) {
                // log a message to the console
                //console.log("Hooray, it worked!");
                window.setTimeout(hide_modal, 1000);
                window.setTimeout(function()
                        {window.location.reload()},1000); 
                });
        // callback handler that will be called on failure
        request.fail(function (jqXHR, textStatus, errorThrown) {
                console.error("The following error occured: " + textStatus, 
                errorThrown);
        });
        // callback handler that will be called regardless
        // if the request failed or succeeded
        request.always(function () {
                // reenable the inputs
                $inputs.prop("disabled", false);
        });            
     }
});

验证凭据的PHP函数是:

function login( array $data )
{
    $_SESSION['logged_in'] = false;
    if( !empty( $data ) ){
        // Trim all the incoming data:
        $trimmed_data = array_map('trim', $data);           
        // escape variables for security
        $email = mysqli_real_escape_string( $this->_con,  
         $trimmed_data['email'] );
        $password = mysqli_real_escape_string( $this->_con,  
         $trimmed_data['password'] );

        if((!$email) || (!$password) ) {
            throw new Exception( LOGIN_FIELDS_MISSING );
        }
        $password = md5( $password );
        $query = "SELECT user_ids, names, emails, createds FROM users where 
         emails = '$email' and passwords = '$password' ";
        $result = mysqli_query($this->_con, $query);
        $data = mysqli_fetch_assoc($result);
        $count = mysqli_num_rows($result);
        if( $count >= 1){
            $_SESSION = $data;
            $_SESSION['logged_in'] = true;
            return true;
        }else{
            return false;
        }
    } else{
        return false;
    }
}

在这里,我面临的问题是,每当用户输入 Wrong Credentials 时,甚至 request.done 中的 alert(textStatus) 都会成功并且模态关闭。

意味着我无法验证用户是否输入了错误的凭据。在提供正确的凭据模式关闭并登录发生。

如何在 .done 函数上按函数检查 return 值。

【问题讨论】:

  • done 将在返回某些内容时触发,无论它是什么。您必须返回登录状态,然后在 done 回调中捕获它并弄清楚接下来要做什么。
  • 这是我无法弄清楚的......根据我知道的概念......但是如何实现这种方式我很困惑。
  • 你能不能只使用成功回调而不是完成?
  • @Ajaypayne .. 你能写一小段代码吗
  • 我建议您验证在.done(...) 回调中收到的响应并验证凭据是否正确,基于此您可以继续登录过程。

标签: javascript php jquery ajax


【解决方案1】:

试试这个例子

在你的 php 脚本中返回 json

<?php
$json=['success'=>'','error'=>''];
//after some action
if(1){
$json['success']='ok';  
}else{
    $json['error']='Your error message!';   
}


echo json_encode($json);
?>

在 JQuery 中设置 dataType json

$.ajax({
    url: '/path/to/file',
    type: 'POST',
    dataType: 'json',
    data: {param1: 'value1'},
})
.done(function(data) {
    if(data.success=='ok'){
    //close the dialog
    }else{
    //show errors
alert(data.error);
or console.log(data.error);
    }
})
.fail(function() {
    console.log("error");
})
.always(function() {
    console.log("complete");
});

这不是您问题的确切答案,但它会帮助您理解我认为的过程!

【讨论】:

    猜你喜欢
    • 2014-01-08
    • 1970-01-01
    • 2016-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-09
    • 2012-07-21
    • 2017-12-15
    相关资源
    最近更新 更多