简体   繁体   English

Ajax调用函数成功返回false无效

[英]Ajax call function on success return false not working

I am running an ajax request, then once I get the result back I choose if it should be continued or if the form should not submit. 我正在运行ajax请求,然后一旦返回结果,就选择是继续还是不提交表单。 I am checking if the email exists. 我正在检查电子邮件是否存在。

Issue is I moved the return false out of the success: as it was not working there and now in a seperate function it is not working either. 问题是我将return false移出了success:由于它在那里不起作用,而现在在单独的函数中,它也不起作用。 I get the alert("FALSE"); 我收到alert("FALSE"); but the form still submits which is no good as I want an error pop up to happen. 但是表单仍然提交,这不好,因为我希望弹出错误。

$.ajax({
  type: "POST",
  url: "/ajax/checkdata.php",
  data: "email="+email,
  success: function(data){
    var returned = true;
    if (data == "Email Exists") {
      returned = false;
    } else {
    }
    emailModal(returned);
  }
})

function emailModal(result){
  if (result) {
    alert("TRUE");
  } else {
    alert("FALSE");
    return false;
  }
}

You'd have to always prevent the form from submitting, and then in the check for the email figure out wether to show an error or submit the form using the native submit handler 您必须始终阻止表单提交,然后在检查电子邮件中确定是否显示错误或使用本提交处理程序提交表单

$('form').on('submit', function(e) {
    e.preventDefault();
    $.ajax({
        type: "POST",
        url: "/ajax/checkdata.php",
        data: {email : email},
        context: this
    }).done(function(data) {
        if (data == "Email Exists") {
            alert(data);
        } else {
            this.submit();
        }
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM