Javascript 如何使 jquery "$.post" 请求同步

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5821380/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 19:03:37  来源:igfitidea点击:

how to make a jquery "$.post" request synchronous

javascriptjqueryhtml

提问by pythonian29033

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.

很长一段时间以来,我一直在使用谷歌搜索并避免在我的错误修复列表中出现此错误,但我终于到达了列表的末尾,最后我必须让函数返回 true/false 以说明是否验证是否成功。

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.

我正在使用 ajax 将某些字段与数据库中已有的字段进行比较,并且默认情况下该$.post()方法异步执行它的操作。

I'm setting a variable inside the call onSuccessand 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 $.postmethod.

我在调用中设置了一个变量onSuccess,因此调用方法没有得到响应,所以我所有的 js/jquery 在 pageLoad 上都失败了......如果我仍然可以继续使用该$.post方法,我更愿意。

回答by Paolo Stefan

jQuery < 1.8

jQuery < 1.8

May I suggest that you use $.ajax()instead of $.post()as it's much more customizable.

我可以建议您使用$.ajax()而不是$.post()因为它更具可定制性。

If you are calling $.post(), e.g., like this:

如果您正在调用$.post(),例如,像这样:

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

You could turn it into its $.ajax()equivalent:

你可以把它变成它的$.ajax()等价物:

$.ajax({
  type: 'POST',
  url: url,
  data: data,
  success: success,
  dataType: dataType,
  async:false
});

Please note the async:falseat the end of the $.ajax()parameter object.

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

Here you have a full detail of the $.ajax()parameters: jQuery.ajax() – jQuery API Documentation.

此处提供了$.ajax()参数的完整详细信息:jQuery.ajax() – jQuery API 文档



jQuery >=1.8 "async:false" deprecation notice

jQuery >=1.8 "async:false" 弃用通知

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:

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

  • use a plugin e.g. BlockUI;
  • manually add an overlay before calling $.ajax(), and then remove it when the AJAX .done()callback is called.
  • 使用插件,例如BlockUI
  • 在调用之前手动添加一个叠加层$.ajax(),然后在调用AJAX.done()回调时将其移除。

Please have a look at this answerfor an example.

请看一下这个答案的例子。

回答by Jason McCreary

If you want an synchronous request set the asyncproperty to falsefor the request. Check out the jQuery AJAX Doc

如果您想要同步请求,请将async属性设置false为请求。查看jQuery AJAX 文档

回答by pythonian29033

From the Jquery docs: you specify the async option to be false to get a synchronous Ajax request. Then your callback can set some data before your mother function proceeds.

从 Jquery 文档中:您将 async 选项指定为 false 以获取同步 Ajax 请求。然后您的回调可以在您的母函数继续之前设置一些数据。

Here's what your code would look like if changed as suggested:

如果按照建议进行更改,您的代码将如下所示:

beforecreate: function(node,targetNode,type,to) {
    jQuery.ajax({
         url:    url,
         success: function(result) {
                      if(result.isOk == false)
                          alert(result.message);
                  },
         async:   false
    });          
}

this is because $.ajax is the only request type that you can set the asynchronousity for

这是因为 $.ajax 是唯一可以设置异步的请求类型