如何制作jquery“$。post”请求同步 [英] how to make a jquery "$.post" request synchronous

查看:82
本文介绍了如何制作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()
$ $ p $ $ $ c $ $ ajax({
类型:'POST',
url: url,
data:data,
success:success,
dataType:dataType,
async:false
});

请注意 async:false $。ajax()参数对象的结尾。



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






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

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


  • 使用插件例如 BlockUI ; 在调用<$ c之前手动添加叠加层$ c> $。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”请求同步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆