首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >通过jqXhr访问Ajax调用响应

通过jqXhr访问Ajax调用响应
EN

Stack Overflow用户
提问于 2015-07-24 10:45:15
回答 1查看 830关注 0票数 1

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

JQUERY调用如下所示

代码语言:javascript
复制
$("#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函数是:

代码语言:javascript
复制
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;
    }
}

在这里,我面临的问题是,每当用户输入错误的凭据时,即使是request.done中的也会成功并关闭模式。

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

如何检查按.done函数返回的.done值。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-07-24 11:04:33

试试这个例子

在php脚本中返回json。

代码语言:javascript
复制
<?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

代码语言:javascript
复制
$.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");
});

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

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31608536

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档