通过jqXhr访问Ajax调用响应PHP。

时间:2022-11-07 01:25:47

I am using Ajax call in Jquery to get a user logged in to the website.

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

JQUERY call looks like as below

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 to verify credentials are:

验证凭证的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;
    }
}

Here, i am facing the issue that whenever user enters Wrong Credentials then even alert(textStatus) in request.done gives success and modal closes.

在这里,我面临的问题是,当用户输入错误的凭证时,即使是在请求中也会发出警报(textStatus)。成功和模式关闭。

Means i am not able to verify whether user has entered wrong credentials. On providing right credentials modal closes and login happpens.

意味着我无法验证用户是否输入了错误的凭证。提供正确的凭证,模态关闭和登录快乐。

How can i check return value by function on .done function.

如何在。done函数上按函数检查返回值。

1 个解决方案

#1


3  

Try this example

试试这个例子

In your php script return json

在php脚本中返回json

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


echo json_encode($json);
?>

In JQuery set dataType json

在JQuery中设置数据类型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");
});

This is not exact answer of your question but it will help you to understand the process I think!

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

#1


3  

Try this example

试试这个例子

In your php script return json

在php脚本中返回json

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


echo json_encode($json);
?>

In JQuery set dataType json

在JQuery中设置数据类型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");
});

This is not exact answer of your question but it will help you to understand the process I think!

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