如何制作jquery“$。post”请求同步 [英] how to make a jquery "$.post" request synchronous
问题描述
我一直在使用googling并避免这个错误在我的错误修复列表中很长一段时间,但是我终于到了列表的最后,我必须让一个函数返回true / false说明验证是否成功。
我使用ajax将某些字段与已经在db中的字段进行比较,并且默认使用 我在调用 jQuery< 1.8 我可以建议您使用 $。post()$
,例如像这样:
onSuccess
中设置了一个变量,而在 $。post
方法。
$。ajax()
而不是<$ c $如果你正在调用 $。post(),那么你可以使用$ c $ $。post()
来定制它。
$。post(url,data,success,dataType);
你可以把它变成它的 $。ajax()$ c
$ $ p $ $ $ c $ $ ajax({
类型:'POST',
url: url,
data:data,
success:success,
dataType:dataType,
async:false
});
请注意 $。ajax()
参数对象的结尾。
这里有 $。ajax()
参数: jQuery.ajax() - jQuery API文档。
jQuery> = 1.8异步:false弃用通知
jQuery> = 1.8不会在http请求期间阻止用户界面,因此只要处理请求,就必须使用解决方法来停止用户交互。例如:
.done()
回调时将其删除。
请查看此答案例如。
I’ve been googling this and avoiding this error in my bug fix list for a long time now, but I’ve finally reached the end of the list, the last of which I have to make a function return true/false to state whether the validation has succeeded or not.
I'm using ajax to compare certain fields with those that are already in the db and by default the $.post()
method does it's operations asynchronously.
I’m setting a variable inside the call onSuccess
and the calling method doesn't get a response because of this, so all my js/jquery fails on pageLoad... I would prefer if I could still keep using the $.post
method.
jQuery < 1.8
May I suggest that you use $.ajax()
instead of $.post()
as it's much more customizable.
If you are calling $.post()
, e.g., like this:
$.post( url, data, success, dataType );
You could turn it into its $.ajax()
equivalent:
$.ajax({
type: 'POST',
url: url,
data: data,
success: success,
dataType: dataType,
async:false
});
Please note the async:false
at the end of the $.ajax()
parameter object.
Here you have a full detail of the $.ajax()
parameters: jQuery.ajax() – jQuery API Documentation.
jQuery >=1.8 "async:false" deprecation notice
jQuery >=1.8 won't block the UI during the http request, so we have to use a workaround to stop user interaction as long as the request is processed. For example:
- use a plugin e.g. BlockUI;
- manually add an overlay before calling
$.ajax()
, and then remove it when the AJAX.done()
callback is called.
Please have a look at this answer for an example.
这篇关于如何制作jquery“$。post”请求同步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!