返回False不工作里面jQuery.ajax [英] Return False not working inside jQuery.ajax

查看:131
本文介绍了返回False不工作里面jQuery.ajax的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WebForm更新用户信息,而当他更新他的电子邮件验证通过阿贾克斯()执行,这样他警告说,如果新的电子邮件地址是已被另一用户使用。

I have a webform for updating user information, and when he updates his email a verification is performed via ajax() so that he is warned if the new email address is already in use by another user.

我试图取消表单提交时,电子邮件是使用,但返回false; 不能正常工作

I'm trying to cancel the form submission when the email is in use, but return false; doesn't work.

任何其他返回false; if语句工作正常范围内,该问题仅是这一个 jQuery.ajax()电话。

Any other return false; within if statements are working fine, the problem is only with this one inside the jQuery.ajax() call.

下面是实际code:

var email = jQuery('#email').val();
jQuery.ajax({
    type : 'GET',
    url : '/ajax/verify-email.php?email=' + email,
    success : function( d ) {
        if( d == '1' ) {
            alert('Another user is using this email');
            jQuery('input[name="email"]').focus();
            return false; // this guy over here is not working!!!!
        }
    }
});

有没有人有办法解决吗?

Does anyone have a solution?

推荐答案

在AJAX的一个实际上是非常重要的。它代表异步。这意味着你触发一个请求到服务器这可能需要一些时间来处理,你以后得到回应。成功的回调里面这种反应发生。但由于这种情况远远晚于实际提交表单,表单实际上已经已经提交前的响应返回。所以返回false从一个AJAX的成功回调是没有任何意义。什么,你想要做的是从表单提交处理程序返回false。让我们看看我们如何能够实现这一点。

The A in AJAX is actually very important. It stands for Asynchronous. This means that you trigger a request to the server which might take some time to process and you get a response later. This response happens inside the success callback. But since this happens much later than the actual form submission, your form has actually been already submitted before the response comes back. So returning false from an AJAX success callback makes no sense whatsoever. What you want to do is to return false from the submit handler of your form. Let's see how we could implement this.

您可以订阅的形式的 .submit 处理程序,并发送一个AJAX请求,以验证是否电子邮件已采取或没有,如果不手动拍摄触发提交表单的成功AJAX的回调里面:

You could subscribe to the .submit handler of the form and send an AJAX request to verify whether the email has already been taken or not and if it is not taken manually trigger the submission of the form inside the success AJAX callback:

$('form').submit(function() {
    // we send an AJAX request to validate the unicity of the email
    $.ajax({
        url: '/ajax/verify-email.php',
        type: 'POST',
        data: { email: $('#email').val() },
        // we set the context to the form so that inside
        // the success callback 'this' points to the form
        context: this,
        success: function(result) {
            if (result != '1') {
                // If the server send something different than 1
                // we know that the email is unique and trigger
                // the submission of the form using the underlying
                // DOM element to avoid calling the .submit handler
                // recusrively
                this.submit();
            } else {
                // The email is not unique => we are informing
                // the user that the email is already in use
                alert('Another user is using this email');
                $('#email').focus();
            } 
        }
    });

    // we cancel the normal submission of the form    
    return false;
});

也从来没有依赖于客户端验证。请确保您正在执行电子邮件是唯一检查一次的形式已成功提交到服务器。如果您正在使用,可以轻松与您的电子邮件字段唯一约束实现SQL数据库。

Also never rely on client side validation. Make sure that you are performing the email is unique check once the form has been successfully submitted to the server. If you are using a SQL database that's easily achieved with a unique constraint on your Email field.

这篇关于返回False不工作里面jQuery.ajax的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆