我一直在向控制台添加日志,以检查不同变量的状态,而不使用Firefox调试器。
然而,在我在main.js文件中添加console.log的许多地方,我收到了以下错误,而不是我给自己的可爱的手写消息:
主线程上的同步XMLHttpRequest已弃用,因为它会对最终用户的体验产生不利影响。更多帮助http://xhr.spec.whatwg.org/
我可以在我的代码使用中添加哪些不会导致此错误的console.log替代品或包装器?
我“做错了”吗?
我一直在向控制台添加日志,以检查不同变量的状态,而不使用Firefox调试器。
然而,在我在main.js文件中添加console.log的许多地方,我收到了以下错误,而不是我给自己的可爱的手写消息:
主线程上的同步XMLHttpRequest已弃用,因为它会对最终用户的体验产生不利影响。更多帮助http://xhr.spec.whatwg.org/
我可以在我的代码使用中添加哪些不会导致此错误的console.log替代品或包装器?
我“做错了”吗?
当前回答
有时候ajax加载一个脚本是必要的,但是延迟文档准备直到脚本加载之后。
jQuery通过holdReady()函数支持这一点。
使用示例:
$.holdReady(true); //set hold
function releaseHold() { $.holdReady(false); } //callback to release hold
$.getScript('script.js', releaseHold); //load script then release hold
实际的脚本加载是异步的(没有错误),但是如果JavaScript的其余部分在文档就绪后运行,那么效果是同步的。
动态脚本通常会使用这个高级特性 加载器,想要加载额外的JavaScript,如jQuery插件 在允许就绪事件发生之前,即使DOM可能发生 准备好了。
文档: https://api.jquery.com/jquery.holdready
2019年1月7日更新
从JQMIGRATE:
jQuery.holdReady() is deprecated Cause: The jQuery.holdReady() method has been deprecated due to its detrimental effect on the global performance of the page. This method can prevent all the code on the page from initializing for extended lengths of time. Solution: Rewrite the page so that it does not require all jQuery ready handlers to be delayed. This might be accomplished, for example, by late-loading only the code that requires the delay when it is safe to run. Due to the complexity of this method, jQuery Migrate does not attempt to fill the functionality. If the underlying version of jQuery used with jQuery Migrate no longer contains jQuery.holdReady() the code will fail shortly after this warning appears.
其他回答
我得到这个异常时,我设置url在查询像“example.com/files/text.txt”。我把url改为“http://example.com/files/text.txt”,这个异常消失了。
对我来说,问题是在OK请求中,我期望ajax响应是一个格式良好的HTML字符串,如表格,但在这种情况下,服务器在请求中遇到了问题,重定向到错误页面,因此返回错误页面的HTML代码(在某处有一个<script标记)。我控制台记录了ajax响应,这时我意识到它不是我所期望的,然后继续进行调试。
在我的情况下,这是由Cloudflare提供的“CDNJS选择”应用程序中的灵活脚本引起的。
根据Cloudflare的说法,“该应用程序已于2015年3月弃用”。我把手机关掉,留言立刻就消失了。
您可以通过访问https://www.cloudflare.com/a/cloudflare-apps/yourdomain.com访问这些应用程序
注意:这是我在这个线程上的答案的副本同步XMLHttpRequest警告和<脚本>(我在寻找解决方案时访问了这两个)
我用以下步骤来解决这个问题:
检查您的CDN脚本并在本地添加它们。 将脚本包含移动到标题部分。
Visual Studio 2015/2017的实时调试器正在注入包含弃用调用的代码。