[英]Tenability of solution to synchronous XMLHttpRequest deprecation warning
This problem has been reported on many forums, including this one, and I've found a solution that is apparently unique, although I'm not sure if it's wise.这个问题在很多论坛上都有报道,包括这个,我找到了一个显然是独一无二的解决方案,虽然我不确定它是否明智。 It certainly does work, but it's probably something someone closer to the JQuery development or one of its members might be able to answer with authority.它确实有效,但它可能是更接近 JQuery 开发的人或其成员之一可能能够权威地回答。
The issue that many others and I have experienced is a console error message with this text:许多其他人和我遇到的问题是带有此文本的控制台错误消息:
Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience.主线程上的同步 XMLHttpRequest 已被弃用,因为它会对最终用户的体验产生不利影响。 For more help, check http://xhr.spec.whatwg.org/如需更多帮助,请查看http://xhr.spec.whatwg.org/
In my case, and in the cases of many others, it references the jquery.js file, specifically this line在我的情况下,在许多其他情况下,它引用了 jquery.js 文件,特别是这一行
xhr.open( options.type, options.url, options.async; options.username, options.password );
The relevant information at http://xhr.spec.whatwg.org/ states: http://xhr.spec.whatwg.org/ 上的相关信息指出:
Developers must not pass false for the async argument when the JavaScript global environment is a document environment.当 JavaScript 全局环境是文档环境时,开发人员不得为 async 参数传递 false。
I have many complex, minified routines written by others that make use of JQuery, and I'm no expert at finding where in those routines the error would be created.我有许多其他人使用 JQuery 编写的复杂的、缩小的例程,我不是专家,不知道在这些例程中会在哪里创建错误。
It then hit me to simply substitute true
for options.async
in the jquery.js file, like this:然后让我在 jquery.js 文件中简单地将options.async
替换为true
,如下所示:
xhr.open( options.type, options.url, true, options.username, options.password );
Voila!瞧! The problem vanished on every browser where the console reported an error.该问题在控制台报告错误的每个浏览器上都消失了。 (Locating and editing the correlative code in the minified JQuery.min.js file was a snap; finding it in all of the other routines that depend on JQuery would have been tedious if not mildly impossible.) (在缩小的 JQuery.min.js 文件中查找和编辑相关代码很容易;在所有其他依赖于 JQuery 的例程中找到它,如果不是不太可能的话,也会很乏味。)
But my question is this: Is this apparent hack of a solution unwise?但我的问题是:这个解决方案的明显破解是不明智的吗? My site does work, and none of the routines that make use of JQuery has a problem with this.我的网站确实有效,并且使用 JQuery 的所有例程都没有问题。 Commentary much appreciated.评论非常感谢。
THE PROBLEM IS IN THE JQUERY DISTRIBUTABLE ITSELF问题在于 JQUERY 可分发本身
The cause of the problem is in the jquery.js file as found in the current version 2.1.4.问题的原因在当前版本 2.1.4 中的 jquery.js 文件中。 Despite documenting options.async
as a deprecated parameter that should only be passed a value of true
, the jquery.js file itself calls the xhr.open file using data defined earlier in the file with the parameter set to false
.尽管将options.async
为只应传递值true
的弃用参数,但 jquery.js 文件本身使用文件中先前定义的数据调用 xhr.open 文件,并将参数设置为false
。 This is the offending routine in the jquery.js file:这是 jquery.js 文件中的违规例程:
jQuery._evalUrl = function( url ) {
return jQuery.ajax({
url: url,
type: "GET",
dataType: "script",
async: false,
global: false,
"throws": true
});
};
When async
is set to true
, the problem vanishes, and the hack solution I had previously used is no longer needed.当async
设置为true
,问题就消失了,并且不再需要我之前使用的 hack 解决方案。
Now, seriously, this is something a true JQuery expert should comment on.现在,严肃地说,这是一个真正的 JQuery 专家应该评论的事情。 Why would the JQuery initialization file violate the very standard of usage that it advises its users to follow?为什么 JQuery 初始化文件会违反它建议用户遵循的使用标准?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.