我该怎么做一个jQuery阻挡AJAX调用,而不异步=假? [英] How do I do a jQuery blocking AJAX call without async = false?
问题描述
我有一个网页,其中不电子邮件的AJAX验证持续到下一个页面,使用HTML5 setCustomValidity()方法[使用webshims库旧的浏览器。在
I have a page which does AJAX validation of an email before continuing on to the next page, using the HTML5 setCustomValidity() method [using the webshims library for older browsers].
对于这项工作,我async选项在$。阿贾克斯()调用,使之同步设置为false,阻塞等待Ajax响应的页面,Ajax调用返回之前,其他形式提交,使验证无效的。
For this to work, I set the async option to false in the $.ajax() call to make it synchronous, blocking the page pending the AJAX response, as otherwise the form submits before the ajax call returns, rendering the validation ineffectual.
<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选项是要pcated在jQuery 1.8代$ P $。
I see that the async option is to be deprecated as of jQuery 1.8.
我怎样才能做到这一点阻击战没有async选项?
How can I achieve this blocking action without the async option?
推荐答案
<一个href="http://bugs.jquery.com/ticket/11013#comment:40">http://bugs.jquery.com/ticket/11013#comment:40
在同步Ajax请求使用的递延/无极功能已经pcated在1.8代$ P $。支持假的,但你必须使用一个回调参数,而不是承诺的方法,如。然后
或 .done <:在$阿贾克斯方法异步/ code>。
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
.
因此,如果您使用的是成功/完整/错误处理程序,您仍然可以使用异步:假
。在jQuery的车票上面的更多信息。
So, if you are using the success/complete/error handlers, you can still use async:false
. More info at the jquery ticket above.
这篇关于我该怎么做一个jQuery阻挡AJAX调用,而不异步=假?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!