微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在没有async = false的情况下执行jQuery阻止AJAX调用?

在使用 HTML5 setCustomValidity()方法[使用旧版浏览器的webshims库]之前,我有一个页面对AJAX进行验证,然后再继续下一页.

为了使这个工作,我在$.ajax()调用中将async选项设置为false,使其同步,阻止页面等待AJAX​​响应,否则表单在ajax调用返回之前提交,使验证无效.

<script>
  function validateEmailRegistered(input) {
    if (input.setCustomValidity === undefined) return;
    var err = 'Email address not found';
    $.ajax({
      url: 'email-is-registered.PHP',data: { email: input.value },async: false,success: function(data) {
        var ok = data=='true';
        input.setCustomValidity(ok ? '' : err)
      }
    });
  }
</script>

<form method="get" action="nextpage.PHP">
  <input name="email" id="email" type="email" required
    onChange="validateEmailRegistered(this)">
  <button type="submit">go</button>
<form>

我看到async选项是不推荐使用的jQuery 1.8.

如何在没有异步选项的情况下实现此阻塞操作?

解决方法

http://bugs.jquery.com/ticket/11013#comment:40

The use of the Deferred/Promise functionality in synchronous ajax requests has been deprecated in 1.8. The $.ajax method with async: false is supported but you must use a callback parameter rather than a Promise method such as .then or .done.

因此,如果您使用成功/完成/错误处理程序,您仍然可以使用async:false.更多信息在上面的jquery票.

原文地址:https://www.jb51.cc/jquery/179970.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐