Javascript jQuery 已弃用同步 XMLHTTPRequest

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27736186/
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 00:35:14  来源:igfitidea点击:

jQuery has deprecated synchronous XMLHTTPRequest

javascriptjqueryajaxxmlhttprequest

提问by Edd

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

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

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

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

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 转移到 WHATWG 的后果吗?它说它发生在2012年。

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?

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

回答by Barmar

To avoid this warning, do not use:

为避免此警告,请勿使用:

async: false

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

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

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.

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

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: falseand 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.

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

Notice that the FetchAPI that's replacing XMLHttpRequestdoes not even offer a synchronous option.

请注意,Fetch要替换的APIXMLHttpRequest甚至不提供同步选项。

回答by Sam

The accepted answer is correct, but I found another cause if you're developing under ASP.NET with Visual Studio 2013 or higher and are sure you didn't make any synchronous ajax requests or define any scripts in the wrong place.

接受的答案是正确的,但如果您使用 Visual Studio 2013 或更高版本在 ASP.NET 下进行开发,并且确定您没有发出任何同步 ajax 请求或在错误的位置定义任何脚本,我发现了另一个原因。

The solution is to disable the "Browser Link" feature by unchecking "Enable Browser Link" in the VS toolbar dropdown indicated by the little refresh icon pointing clockwise. As soon as you do this and reload the page, the warnings should stop!

解决方案是通过取消选中 VS 工具栏下拉菜单中的“启用浏览器链接”来禁用“浏览器链接”功能,由顺时针指向的小刷新图标指示。一旦你这样做并重新加载页面,警告就会停止!

Disable Browser Link

禁用浏览器链接

This should only happen while debugging locally, but it's still nice to know the cause of the warnings.

这应该只在本地调试时发生,但知道警告的原因仍然很好。

回答by Dave Friend

This happened to me by having a link to external js outside the head just before the end of the body section. You know, one of these:

这发生在我身上,因为在正文部分结束之前有一个指向头部外部的外部 js 的链接。你知道,其中之一:

<script src="http://somesite.net/js/somefile.js">

<script src="http://somesite.net/js/somefile.js">

It did not have anything to do with JQuery.

它与 JQuery 没有任何关系。

You would probably see the same doing something like this:

你可能会看到同样的事情做这样的事情:

var script = $("<script></script>");
script.attr("src", basepath + "someotherfile.js");
$(document.body).append(script);

But I haven't tested that idea.

但我还没有测试过这个想法。

回答by Hugo Delsing

It was mentioned as a comment by @henri-chan, but I think it deserves some more attention:

@henri-chan在评论中提到它,但我认为它值得更多关注:

When you update the content of an element with new html using jQuery/javascript, and this new html contains <script>tags, those are executed synchronouslyand thus triggering this error. Same goes for stylesheets.

当您使用 jQuery/javascript 用新的 html 更新元素的内容,并且这个新的 html 包含<script>标签时,这些标签会同步执行,从而触发此错误。样式表也是如此。

You know this is happening when you see (multiple) scripts or stylesheets being loaded as XHRin the console window. (firefox).

当您看到XHR在控制台窗口中加载(多个)脚本或样式表时,您就知道正在发生这种情况。(火狐)。

回答by Adib Aroui

No one of the previous answers (which all are correct) was suited to my situation: I don't use the asyncparameter in jQuery.ajax()and I don't include a script tag as part of the content that was being returned like:

以前的答案(都是正确的)没有一个适合我的情况:我不使用async参数 injQuery.ajax()并且我不包含脚本标记作为返回内容的一部分,例如:

<div> 
     SOME CONTENT HERE
</div>
<script src="/scripts/script.js"></script> 

My situation is that I am calling two AJAX requests consecutively with the aim to update two divs at the same time:

我的情况是我连续调用两个 AJAX 请求,目的是同时更新两个 div:

function f1() {
     $.ajax(...); // XMLHTTP request to url_1 and append result to div_1
}

function f2() {
     $.ajax(...); // XMLHTTP request to url_2 and append result to div_2
}

function anchor_f1(){
$('a.anchor1').click(function(){
     f1();
})
}

function anchor_f2(){
$('a.anchor2').click(function(){
     f2();
});
}

// the listener of anchor 3 the source of problem
function anchor_problem(){
$('a.anchor3').click(function(){
     f1();
     f2();
});
}

anchor_f1();
anchor_f2();
anchor_problem();

When I click on a.anchor3, it raises the warning flag.I resolved the issue by replacing f2 invoking by click()function:

当我点击时a.anchor3,它会引发警告标志。我通过替换 f2 调用click()功能解决了这个问题:

function anchor_problem(){
$('a.anchor_problem').click(function(){
     f1();
     $('a.anchor_f2').click();
});
}

回答by runback

My workabout: I use asynchronous requests dumping the code to a buffer. I have a loop checking the buffer every second. When the dump has arrived to the buffer I execute the code. I also use a timeout. For the end user the page works as if synchronous requests would be used.

我的工作:我使用异步请求将代码转储到缓冲区。我每秒都有一个循环检查缓冲区。当转储到达缓冲区时,我执行代码。我也使用超时。对于最终用户,该页面就像使用同步请求一样工作。