如何制作jquery“$。post”请求同步

编程入门 行业动态 更新时间:2024-10-10 11:22:34
本文介绍了如何制作jquery“$。post”请求同步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我一直在使用googling并避免这个错误在我的错误修复列表中很长一段时间,但是我终于到了列表的最后,我必须让一个函数返回true / false说明验证是否成功。

我使用ajax将某些字段与已经在db中的字段进行比较,并且默认使用 $。post()我在调用 onSuccess 中设置了一个变量,而在方法中它是异步操作。调用方法没有得到响应,因此,所有我的js / jquery在pageLoad失败...我宁愿如果我仍然可以继续使用 $。post 方法。

解决方案

jQuery< 1.8

我可以建议您使用 $。ajax()而不是<$ c $如果你正在调用 $。post(),那么你可以使用$ c $ $。post()来定制它。

,例如像这样:

$。post(url,data,success,dataType);

你可以把它变成它的 $。ajax()请注意 async:false $。ajax()参数对象的结尾。

这里有 $。ajax()参数: jQuery.ajax() - jQuery API文档。

jQuery> = 1.8异步:false弃用通知

jQuery> = 1.8不会在http请求期间阻止用户界面,因此只要处理请求,就必须使用解决方法来停止用户交互。例如:

  • 使用插件例如 BlockUI ; 在调用 $。ajax(),然后在调用AJAX .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”请求同步

本文发布于:2023-10-10 21:59:40,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:jquery   post

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!