无法获取函数的ajax回调

时间:2015-02-26 05:39:27

标签: javascript jquery ajax

我有一张表格供用户注册新帐户。我使用jquery + ajax检查表单提交时的电子邮件地址的可用性。在Jquery代码中,我使用了e.preventDefault();如果发生任何错误,请阻止表单提交。我在电子邮件输入中尝试了现有的电子邮件地址,然后单击“提交表单”。它允许表单提交。它不应该这样做,因为ajax reponseText返回true表示电子邮件地址已经存在于数据库中。

有人可以告诉我如何修复我的代码,这样如果ajax响应返回true,它将阻止表单提交并显示错误。

我尝试阅读并关注this article但经过这么多尝试后失败了。

这是我的表格:

<form role="form" method="post" id="signupForm" action="index.php?view=signup-gv">
                <div class="col-xs-6 border-right">
                    <div class="form-group">
                        <label for="exampleInputEmail1">Full Name</label>
                        <input type="text" class="form-control" id="regname" name="regname" placeholder="Full Name">
                    </div>
                    <div class="form-group">
                        <label for="exampleInputEmail1">Email Address</label><span id="emailcheck"></span>
                        <input type="email" class="form-control" id="regemail" name="regemail" placeholder="Enter email">
                    </div>
                </div>
                <div class="form-group col-xs-6">
                    <label for="exampleInputPassword1">Password</label>
                    <input type="password" class="form-control" id="regpass" name="regpass" placeholder="Password">
                </div>
                <button style="position:relative; left: 15px; top: 10px;" class="btn btn-default" name="register" id="register">Register</button>
            </form>

这是我的jquery代码:

$(document).ready(function(){
$('#regname').focus(); 
$('#signupForm').submit(function(e) {
var regname = $('#regname'); 
var regemail = $('#regemail'); 
var regpass = $('#regpass'); 
var register_result = $('#register_result'); 
register_result.html('Loading..'); 
if(regname.val() == ''){ 
    regname.focus();
    register_result.html('<span class="errorss"> * Full name can not be blank</span>');
    e.preventDefault();
}
else if ($.trim(regemail.val()).length == 0) {
    regemail.focus(); 
    register_result.html('<span class="errorss">* Email address can not be blank</span>');
    e.preventDefault();
}
else if(regpass.val() == ''){ 
    regpass.focus();
    register_result.html('<span class="errorss">* Password can not be blank</span>');
    e.preventDefault();
}
emailCheck().done(function(r){
    if(r){
        $('#regemail').focus(); 
        $('#register_result').html('<span class="errorss"> This email address is already existed. Please choose another one </span>');
        e.preventDefault();
        }
    }); 

});
});
function emailCheck() {
var regemail = $('#regemail'); 
var emailcheck = $('#emailcheck');
emailcheck.html(''); 
var UrlToPass = {regemail:regemail.val()} ;
    $.ajax({ 
    type : 'POST',
    cache: false,
    data : UrlToPass,
    url  : 'emailcheck.php',
    success: function(responseText){ 
        if(responseText == 0){
            return false; // good to go
        }
        else{
            emailcheck.html('<span class="errorss"> This email is existed.</span>');
            return true; // This email is registered. Please try different one
        }
    }
    });

}

2 个答案:

答案 0 :(得分:0)

您对同步和异步功能感到困惑。 ajax函数进行异步调用并在其回调中返回输出。您正在尝试将Async函数包装在普通函数中并期望它同步运行。

您的函数在Ajax调用收到其输出之前返回。使用

async:false

$.ajax({ 
    type : 'POST',
    cache: false,
     async: false,
    data : UrlToPass,

请参阅以下内容:

How to make JQuery-AJAX request synchronous

答案 1 :(得分:0)

首先,你没有从emailCheck()函数返回任何内容,但是你正在使用它,好像它正在返回一个promise对象。

所以

$(document).ready(function () {
    $('#regname').focus();
    $('#signupForm').submit(function (e) {
        var regname = $('#regname');
        var regemail = $('#regemail');
        var regpass = $('#regpass');
        var register_result = $('#register_result');
        register_result.html('Loading..');
        //prevent the form submit
        e.preventDefault();

        if (regname.val() == '') {
            regname.focus();
            register_result.html('<span class="errorss"> * Full name can not be blank</span>');
        } else if ($.trim(regemail.val()).length == 0) {
            regemail.focus();
            register_result.html('<span class="errorss">* Email address can not be blank</span>');
        } else if (regpass.val() == '') {
            regpass.focus();
            register_result.html('<span class="errorss">* Password can not be blank</span>');
        } else {
            emailCheck().done(function (r) {
                if (r) {
                    $('#regemail').focus();
                    $('#register_result').html('<span class="errorss"> This email address is already existed. Please choose another one </span>');
                } else {
                    $('#signupForm')[0].submit();
                }
            });
        }
    });
});

function emailCheck() {
    var regemail = $('#regemail');
    var emailcheck = $('#emailcheck');
    emailcheck.html('');
    var UrlToPass = {
        regemail: regemail.val()
    };
    var deferred = jQuery.Deferred();
    $.ajax({
        type: 'POST',
        cache: false,
        data: UrlToPass,
        url: 'emailcheck.php',
        success: function (responseText) {
            if (responseText == 0) {
                deferred.resolve(false);
            } else {
                emailcheck.html('<span class="errorss"> This email is existed.</span>');
                deferred.resolve(true);
            }
        },
        error: function () {
            deferred.reject();
        }
    });
    return deferred.promise();
}