jQuery ajax async: false 导致一个奇怪的警告?
jQuery ajax async: false causes a strange warning?
在我的 JS 应用程序中,我有很多 Ajax 调用 async: false
。我正在使用最新的 Chrome 浏览器,最近在我的控制台中出现以下警告。
Synchronous XMLHttpRequest on the main thread is deprecated because of
its detrimental effects to the end user's experience. For more help,
check http://xhr.spec.whatwg.org/.
现在我正在尝试将所有 async: false
更改为 async: true
。但这会导致很多错误,因为我的应用需要 运行 和 async: false
。
警告只是错误还是什么?我可以担心还是忽略它?
它告诉您,如果服务器关闭,您 运行 有锁定浏览器的风险。
建立一个带有取消按钮的模态对话框并异步调用函数。
Is the warning just a bug or something?
不,这不是错误。由于消息中所述的原因,同步请求确实已被弃用。
Can i worry about that or ignore that?
您应该听取建议并对您的应用程序进行适当的更改(是的,这并不像将 async: false
更改为 async: true
那样简单)。
也许这有助于更好地理解异步操作:
- Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference
- How do I return the response from an asynchronous call?
此消息告诉您 async: false
造成了糟糕的用户体验。这是因为它会在 ajax 调用期间锁定浏览器。这不是错误,而是一条建议消息,说明您正在做一些您可能不应该做的事情。
我很少看到对 async: false
的合法需求,但是使用 async: true
编程(以创建更好的用户体验)需要编写与异步操作一起使用的代码。这是您必须学习如何做的事情,但是一旦学会就非常可行。
您不能简单地同步写入 运行 的代码并将标志更改为异步并期望同步代码能够工作。相反,您必须更改代码以处理异步响应,并且您通常必须重构一些使用响应的代码,使其位于异步响应处理程序中。
如果您想了解如何开始使用异步编码 ajax,那么我建议您阅读这个问题及其各种答案:How do I return the response from an asynchronous call?
JavaScript 在 UI 线程上独占运行,因此同步 AJAX 调用将完全冻结浏览器,直到服务器回复。
在 Web 应用程序的 UX 设计中,这被认为是一种不好的做法,主要浏览器正在弃用此功能,转而支持带有回调的异步请求。
You may not worry about the message now. It's not a bug but I strongly recommend you to start rewriting it because when a feature is marked as deprecated it means a warn that they can remove this feature anytime in the future.
它也从 jQuery 1.8+
中弃用
As of jQuery 1.8, the use of async: false with jqXHR ($.Deferred) is
deprecated; you must use the success/error/complete callback options
instead of the corresponding methods of the jqXHR object such as
jqXHR.done() or the deprecated jqXHR.success()
推荐方式:
var request = $.ajax({
url: "script.php",
data: { id : menuId },
dataType: "application/json"
});
request.done(function(data) {
// Executed in case of success
});
request.fail(function( jqXHR, textStatus ) {
// Executed in case of error
});
Is the warning just a bug or something?
警告 本身 不是错误。它警告您您的代码中可能存在错误。您的代码使用的功能已被弃用,这意味着它能按您预期的方式工作的保证越来越少。警告很简单,嗯,警告你这个。
Can i worry about that or ignore that?
忽略警告通常不是一个好主意。当它们最终成为错误时,您只能为停机时间负责。
But it causes a lot of error because my app needs to run with async: false
你在这里混淆了 cause/effect 关系。 async: true
不会导致错误,而是代码。这些错误应该被纠正,而不是被规避。通过使用 async: false
来规避错误只是将修复它们推迟到以后。此警告告诉您 "later" 即将来临,并建议您在问题变得更糟之前解决问题。
如果您的应用 "needs to run with async: false
" 那么您的应用设计不正确。该警告建议您修复它。这里的其他人也是。
在我的 JS 应用程序中,我有很多 Ajax 调用 async: false
。我正在使用最新的 Chrome 浏览器,最近在我的控制台中出现以下警告。
Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help, check http://xhr.spec.whatwg.org/.
现在我正在尝试将所有 async: false
更改为 async: true
。但这会导致很多错误,因为我的应用需要 运行 和 async: false
。
警告只是错误还是什么?我可以担心还是忽略它?
它告诉您,如果服务器关闭,您 运行 有锁定浏览器的风险。
建立一个带有取消按钮的模态对话框并异步调用函数。
Is the warning just a bug or something?
不,这不是错误。由于消息中所述的原因,同步请求确实已被弃用。
Can i worry about that or ignore that?
您应该听取建议并对您的应用程序进行适当的更改(是的,这并不像将 async: false
更改为 async: true
那样简单)。
也许这有助于更好地理解异步操作:
- Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference
- How do I return the response from an asynchronous call?
此消息告诉您 async: false
造成了糟糕的用户体验。这是因为它会在 ajax 调用期间锁定浏览器。这不是错误,而是一条建议消息,说明您正在做一些您可能不应该做的事情。
我很少看到对 async: false
的合法需求,但是使用 async: true
编程(以创建更好的用户体验)需要编写与异步操作一起使用的代码。这是您必须学习如何做的事情,但是一旦学会就非常可行。
您不能简单地同步写入 运行 的代码并将标志更改为异步并期望同步代码能够工作。相反,您必须更改代码以处理异步响应,并且您通常必须重构一些使用响应的代码,使其位于异步响应处理程序中。
如果您想了解如何开始使用异步编码 ajax,那么我建议您阅读这个问题及其各种答案:How do I return the response from an asynchronous call?
JavaScript 在 UI 线程上独占运行,因此同步 AJAX 调用将完全冻结浏览器,直到服务器回复。
在 Web 应用程序的 UX 设计中,这被认为是一种不好的做法,主要浏览器正在弃用此功能,转而支持带有回调的异步请求。
You may not worry about the message now. It's not a bug but I strongly recommend you to start rewriting it because when a feature is marked as deprecated it means a warn that they can remove this feature anytime in the future.
它也从 jQuery 1.8+
中弃用As of jQuery 1.8, the use of async: false with jqXHR ($.Deferred) is deprecated; you must use the success/error/complete callback options instead of the corresponding methods of the jqXHR object such as jqXHR.done() or the deprecated jqXHR.success()
推荐方式:
var request = $.ajax({
url: "script.php",
data: { id : menuId },
dataType: "application/json"
});
request.done(function(data) {
// Executed in case of success
});
request.fail(function( jqXHR, textStatus ) {
// Executed in case of error
});
Is the warning just a bug or something?
警告 本身 不是错误。它警告您您的代码中可能存在错误。您的代码使用的功能已被弃用,这意味着它能按您预期的方式工作的保证越来越少。警告很简单,嗯,警告你这个。
Can i worry about that or ignore that?
忽略警告通常不是一个好主意。当它们最终成为错误时,您只能为停机时间负责。
But it causes a lot of error because my app needs to run with
async: false
你在这里混淆了 cause/effect 关系。 async: true
不会导致错误,而是代码。这些错误应该被纠正,而不是被规避。通过使用 async: false
来规避错误只是将修复它们推迟到以后。此警告告诉您 "later" 即将来临,并建议您在问题变得更糟之前解决问题。
如果您的应用 "needs to run with async: false
" 那么您的应用设计不正确。该警告建议您修复它。这里的其他人也是。