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

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

问题描述

我一直在控制台添加日志来检查不同变量的状态,而不使用Firefox调试器。

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

然而,在我的 main中添加了一个 console.log 的许多地方.js 文件,我收到以下错误,而不是我可爱的小手写邮件给我自己:

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:


同步XMLHttpRequest主线程由于对最终用户体验的不利影响而被淘汰。更多帮助 http://xhr.spec.whatwg.org/

可以将 console.log 的替代方法或包装器添加到我的代码使用,不会导致此错误?

What alternatives to or wrappers for console.log can I add to my code use that will not cause this error?

我是做错了吗?

推荐答案

当我被懒惰时,发生在我身上,并将脚本标签作为返回内容的一部分。因此:

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:

部分HTML内容:

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

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

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.

在这样的情况下,您可以通过查找某种绑定框架,只返回一个JSON对象,或者根据您的后端和模板化来更好地服务可以改变你如何加载你的脚本。

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.

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

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.

http://jsfiddle.net/49tkL0qd/

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

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