JavaScript console.log 导致错误:“不推荐使用主线程上的同步 XMLHttpRequest..."; [英] JavaScript console.log causes error: "Synchronous XMLHttpRequest on the main thread is deprecated..."

查看:29
本文介绍了JavaScript console.log 导致错误:“不推荐使用主线程上的同步 XMLHttpRequest...";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在向控制台添加日志,以在不使用 Firefox 调试器的情况下检查不同变量的状态.

但是,在我在 main.js 文件中添加 console.log 的许多地方,我收到以下错误而不是我可爱的小手写消息我自己:

<块引用>

主线程上的同步 XMLHttpRequest 已被弃用,因为它会对最终用户的体验产生不利影响.如需更多帮助http://xhr.spec.whatwg.org/

console.log 的哪些替代方案或包装器可以添加到我的代码使用中而不会导致此错误?

我做错了"吗?

解决方案

这发生在我懒惰的时候,我在返回的内容中包含了一个脚本标记.因此:

部分 HTML 内容:

这里的一些内容

<script src="/scripts/script.js"></script>

看来,至少在我的情况下,如果您通过 xhr 返回这样的 HTML 内容,您将导致 jQuery 调用以获取该脚本.该调用发生在异步标志 false 的情况下,因为它假定您需要脚本继续加载.

在这种情况下,您最好查看某种绑定框架并仅返回一个 JSON 对象,或者根据您的后端和模板,您可以更改加载脚本的方式.

你也可以使用 jQuery 的 getScript() 来抓取相关脚本.这是一个小提琴,它只是 jQuery 示例的直接副本,但是当以这种方式加载脚本时,我没有看到任何警告.

示例

http://jsfiddle.net/49tkL0qd/

I have been adding logs to the console to check the status of different variables without using the Firefox debugger.

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:

Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help 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?

Am I "doing it wrong"?

解决方案

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:

<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. That call happens with an async flag false since it assumes you need the script to continue loading.

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.

You could also use jQuery's getScript() to grab relevant scripts. 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.

Example

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

http://jsfiddle.net/49tkL0qd/

这篇关于JavaScript console.log 导致错误:“不推荐使用主线程上的同步 XMLHttpRequest...";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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