不推荐使用 jQuery.ajax() 方法的异步选项,现在怎么办?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11448011/
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
jQuery.ajax() method's async option deprecated, what now?
提问by Juw
As of jQuery 1.8, the use of async:false
in jQuery.ajax()is deprecated.
But how many webpages have you seen with a "loading screen" while there is an ongoing AJAX communication in the background? I have probably seen thousands of them.
从 jQuery 1.8 开始,不推荐async:false
在jQuery.ajax()中使用。
但是,当后台有正在进行的 AJAX 通信时,您看到了多少带有“加载屏幕”的网页?我可能见过成千上万的人。
My case is that I am writing a mobile app that needs to load a language file. And at the beginning I load the language file and I retrieve the text of the buttons and other GUI elements from the language file.
我的情况是我正在编写一个需要加载语言文件的移动应用程序。一开始我加载语言文件并从语言文件中检索按钮和其他 GUI 元素的文本。
This is really bad for me. Because if the language file is missing, the GUI shouldn't appear. So how do I solve it? Put all my code in the success
callback? That doesn′t seem like a good coding practice to me. Can I solve it another way?
这对我来说真的很糟糕。因为如果缺少语言文件,则不应出现 GUI。那么我该如何解决呢?把我所有的代码都放在success
回调中?对我来说,这似乎不是一个好的编码习惯。我可以用另一种方式解决它吗?
采纳答案by J?rgen R
The solution is to manually add an overlay to prevent the user to interact with the interface, and then remove it once the AJAX query is done.
解决方案是手动添加一个overlay来防止用户与界面交互,然后在AJAX查询完成后将其删除。
$(function() {
show_overlay();
$.ajax({
// Query to server
}).done(function() {
// Verify good data
// Do stuff
remove_overlay();
});
});
回答by Infeligo
I read the official discussionin the ticket about the deprecation of this parameter and here is what I understood:
我阅读了工单中关于弃用此参数的官方讨论,这是我的理解:
The problem is that implementing Promises (1) for sync AJAX gives them overhead.
There are tons of real world use cases of sync AJAX, e.g. preserving state before page unload. Therefore, this functionality will stay, but the way you use it may change.
The nearest solution (landing in 1.8?) is to support only callbacks (but not the Promises) when
async
isfalse
.
有大量同步 AJAX 的实际用例,例如在页面卸载之前保留状态。因此,此功能将保留,但您使用它的方式可能会改变。
最近的溶液(1.8?降落)是只支持回调(但不承诺),当
async
是false
。
To conclude: Keep using async: false
if you have to, but beware of its drawbacks (blocking of VM). Don't worry, you will be provided an alternative if this feature ever gets removed form $.ajax()
.
结论:async: false
如果必须,请继续使用,但要注意它的缺点(VM 阻塞)。别担心,如果此功能从 form 中删除,您将获得另一种选择$.ajax()
。
回答by David
I would bet that manyof those 1000's of pages don't actually block the UI while waiting for the AJAX call. Instead, they probably obscure the UI with the waiting screen at the time the call is made and then remove that on a response handler.
我敢打赌,在等待 AJAX 调用时,这 1000 个页面中的许多页面实际上并没有阻塞 UI。相反,他们可能会在调用时用等待屏幕模糊 UI,然后在响应处理程序上将其删除。
There are many ways to obscure the UI (you could even just use a jQuery UI Dialog that's set to Modal and has no escape or close buttons), so I'll leave that decision up to you. But the layout of the code would be something like this:
有很多方法可以隐藏 UI(您甚至可以使用设置为 Modal 且没有退出或关闭按钮的 jQuery UI 对话框),因此我将决定由您决定。但是代码的布局是这样的:
var someFunction = function () {
// any pre-conditions to the logic
// obscure the UI here
$.ajax({
url: 'ajax/test.html',
success: function(data) {
// handle the response
// show the UI again
},
error: function(data) {
// handle the response
// show the UI again
}
});
}
I'm sure there are multiple ways to achieve that order of events, but that's the general idea. Blocking the UI was never really the intent, and I imagine it was an even more difficult decision for jQuery to includethat feature than it was to removeit. It's meant to be asynchronous.
我确信有多种方法可以实现事件的顺序,但这是总体思路。阻止 UI 从来都不是真正的意图,我想 jQuery包含该功能比删除它更困难。它意味着是异步的。
回答by Ariel
Why would you use ajax to get this file? Just include it using a script
tag.
为什么要使用ajax来获取这个文件?只需使用script
标签包含它。
In any case, you don't put all your code in the onSuccess - instead you call a single function from there that starts your code running.
在任何情况下,您都不会将所有代码都放在 onSuccess 中——而是从那里调用一个函数来启动您的代码运行。