JavaScript console.log导致错误:“不赞成在主线程上使用同步XMLHttpRequest…”

本文翻译自:JavaScript console.log causes error: “Synchronous XMLHttpRequest on the main thread is deprecated…”

I have been adding logs to the console to check the status of different variables without using the Firefox debugger. 我一直在向控制台添加日志,以在不使用Firefox调试器的情况下检查不同变量的状态。

However, in many places in which I add a console.log in my main.js file, I receive the following error instead of my lovely little handwritten messages to myself: 但是,在许多在main.js文件中添加console.logmain.js ,我收到以下错误,而不是给我自己的一些可爱的手写消息:

Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. 不赞成在主线程上使用同步XMLHttpRequest,因为它会对最终用户的体验产生不利影响。 For more help http://xhr.spec.whatwg.org/ 有关更多帮助,请访问http://xhr.spec.whatwg.org/

What alternatives to or wrappers for console.log can I add to my code use that will not cause this error? 我可以在使用的代码中添加console.log替代品或包装,而不会导致此错误?

Am I "doing it wrong"? 我“做错了”吗?


#1楼

参考:https://stackoom.com/question/1fNod/JavaScript-console-log导致错误-不赞成在主线程上使用同步XMLHttpRequest


#2楼

The warning message MAY BE due to an XMLHttpRequest request within the main thread with the async flag set to false. 该警告消息可能是由于主线程中的XMLHttpRequest请求将async标志设置为false所致。

https://xhr.spec.whatwg.org/#synchronous-flag : https://xhr.spec.whatwg.org/#synchronous-flag

Synchronous XMLHttpRequest outside of workers is in the process of being removed from the web platform as it has detrimental effects to the end user's experience. 工作者外部的同步XMLHttpRequest正在从Web平台中删除,因为它会对最终用户的体验产生不利影响。 (This is a long process that takes many years.) Developers must not pass false for the async argument when the JavaScript global environment is a document environment. (这是一个耗时多年的漫长过程。)当JavaScript全局环境是文档环境时,开发人员不得为async参数传递false。 User agents are strongly encouraged to warn about such usage in developer tools and may experiment with throwing an InvalidAccessError exception when it occurs. 强烈建议用户代理警告开发人员工具中的此类用法,并可以尝试在发生InvalidAccessError异常时抛出该异常。

The future direction is to only allow XMLHttpRequests in worker threads. 未来的方向是只允许在工作线程中使用XMLHttpRequests。 The message is intended to be a warning to that effect. 该消息旨在作为对此的警告。


#3楼

This happened to me when I was being lazy and included a script tag as part of the content that was being returned. 当我很懒惰时,这发生在我身上,并且在返回的内容中包含了一个脚本标记。 As such: 因此:

Partial HTML Content: 部分HTML内容:

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

It appears, at least in my case, that if you return HTML content like that via xhr, you will cause jQuery to make a call to get that script. 至少在我看来,如果您通过xhr返回类似HTML内容,则将导致jQuery进行调用以获取该脚本。 That call happens with an async flag false since it assumes you need the script to continue loading. 该调用发生在异步标志为false的情况下,因为它假定您需要脚本才能继续加载。

In situations like this one you'd be better served by looking into a binding framework of some kind and just returning a JSON object, or depending on your backend and templating you could change how you load your scripts. 在这种情况下,最好通过研究某种绑定框架并仅返回JSON对象,或者根据后端和模板来改变加载脚本的方式,从而更好地为您服务。

You could also use jQuery's getScript() to grab relevant scripts. 您也可以使用jQuery的getScript()获取相关脚本。 Here is a fiddle, It's just a straight copy of the jQuery example, but I'm not seeing any warnings thrown when scripts are loaded that way. 这是一个小提琴,它只是jQuery示例的直接副本,但是以这种方式加载脚本时,我看不到任何警告。

Example

<script>
var url = "/scripts/script.js";
$.getScript(url);
</script>

http://jsfiddle.net/49tkL0qd/ http://jsfiddle.net/49tkL0qd/


#4楼

I was also facing same issue but able to fix it by putting async: true. 我也面临着同样的问题,但是能够通过放置async来解决它:true。 I know it is by default true but it works when I write it explicitly 我知道默认情况下它是true,但是当我明确编写它时它可以工作

$.ajax({
   async: true,   // this will solve the problem
   type: "POST",
   url: "/Page/Method",
   contentType: "application/json",
   data: JSON.stringify({ ParameterName: paramValue }),
});

#5楼

I got this exception when I set url in query like "example.com/files/text.txt". 当我在“ example.com/files/text.txt”之类的查询中设置网址时,出现了此异常。 Ive changed url to " http://example.com/files/text.txt " and this exception dissapeared. 我已经将网址更改为“ http://example.com/files/text.txt ”,并且此异常消失了。


#6楼

In my case this was caused by the flexie script which was part of the "CDNJS Selections" app offered by Cloudflare . 在我的情况下,这是由Flexfla脚本引起的,该脚本是Cloudflare提供的“ CDNJS Selections”应用程序的一部分。

According to Cloudflare "This app is being deprecated in March 2015". 根据Cloudflare的说法,“此应用将于2015年3月弃用”。 I turned it off and the message disappeared instantly. 我将其关闭,该消息立即消失。

You can access the apps by visiting https://www.cloudflare.com/a/cloudflare-apps/yourdomain.com 您可以通过访问https://www.cloudflare.com/a/cloudflare-apps/yourdomain.com来访问应用程序

NB: this is a copy of my answer on this thread Synchronous XMLHttpRequest warning and <script> (I visited both when looking for a solution) 注意:这是我对此线程的回答的副本同步XMLHttpRequest警告和<script> (我在寻找解决方案时都访问了)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值