jQuery 已弃用同步 XMLHTTPRequest [英] jQuery has deprecated synchronous XMLHTTPRequest

查看:31
本文介绍了jQuery 已弃用同步 XMLHTTPRequest的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

像许多其他人一样,我的网站使用 jQuery.当我打开开发人员工具时,我看到一条警告说 XMLHTTPRequest 是

Like many others, my website is using jQuery. When I open the developer tools, I see a warning that says that XMLHTTPRequest is

已弃用,因为它会对最终用户的体验产生不利影响.

deprecated because of its detrimental effects to the end user's experience.

我继续阅读了文档的一部分,但它的技术性很强.有人可以用简单的术语解释从 XMLHTTPRequest 转移到 WHATWG 的后果吗?它说它发生在 2012 年.

I went on and read part of the documentation, but it was fairly technical. Can someone explain the consequences of shifting from XMLHTTPRequest to WHATWG in simple terms? It says it happened in 2012.

此外,文档说工作人员之外的同步 XMLHttpRequest 正在从网络平台中删除,当发生这种情况时,如果用户代理在服务中使用它们,他们是否需要修改现有代码?

Also, the documentation says that Synchronous XMLHttpRequest outside of workers is in the process of being removed from the web platform, when that happens, if a user agent had them in a service, do they need to modify their existing code?

推荐答案

为了避免这个警告,不要使用:

To avoid this warning, do not use:

async: false

在您的任何 $.ajax() 调用中.这是 XMLHttpRequest 唯一不推荐使用的功能.

in any of your $.ajax() calls. This is the only feature of XMLHttpRequest that's deprecated.

默认值为 async: true,因此如果您根本不使用此选项,那么如果该功能被真正删除,您的代码应该是安全的.

The default is async: true, so if you never use this option at all, your code should be safe if the feature is ever really removed.

然而,它可能不会——它可能会从标准中删除,但我敢打赌浏览器将继续支持它很多年.因此,如果您出于某种原因确实需要同步 AJAX,您可以使用 async: false 并忽略警告.但是同步 AJAX 被认为是糟糕的风格是有充分理由的,因此您可能应该尝试找到一种方法来避免它.编写 Flash 应用程序的人可能也从未想过它会消失,但现在正在逐步淘汰.

However, it probably won't be -- it may be removed from the standards, but I'll bet browsers will continue to support it for many years. So if you really need synchronous AJAX for some reason, you can use async: false and just ignore the warnings. But there are good reasons why synchronous AJAX is considered poor style, so you should probably try to find a way to avoid it. And the people who wrote Flash applications probably never thought it would go away, either, but it's in the process of being phased out now.

请注意,替换 XMLHttpRequestFetch API 甚至不提供同步选项.

Notice that the Fetch API that's replacing XMLHttpRequest does not even offer a synchronous option.

这篇关于jQuery 已弃用同步 XMLHTTPRequest的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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