【问题标题】:Synchronous XMLHttpRequest on the main thread is deprecated不推荐使用主线程上的同步 XMLHttpRequest
【发布时间】:2015-06-11 11:17:00
【问题描述】:
  • 我正在开发 extjs 框架..
    • 我有一个在 extjs 上运行的应用程序..
    • 每当我在浏览器中打开应用程序时...我都会看到以下内容 我的控制台中的警告...

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

  • 你们能告诉我如何删除它吗..
  • 当我点击 ext-all-debug-w-cmets.js 文件的警告时
  • 并指向以下代码部分...

    试试{ xhr.open('GET', noCacheUrl, false); xhr.send(null); } 抓住 (e) { isCrossOriginRestricted = true; }

  • 你们能告诉我如何防止它发生吗...

  • 在该文件下方提供我的代码

    /** * 加载脚本文件,支持异步和同步两种方式 * @私人的 */ loadScriptFile:函数(url,onLoad,onError,范围,同步){ if (isFileLoaded[url]) { 返回装载机; }

            var config = Loader.getConfig(),
                noCacheUrl = url + (config.disableCaching ? ('?' + config.disableCachingParam + '=' + Ext.Date.now()) : ''),
                isCrossOriginRestricted = false,
                xhr, status, onScriptError,
                debugSourceURL = "";
    
            scope = scope || Loader;
    
            Loader.isLoading = true;
    
            if (!synchronous) {
                onScriptError = function() {
                };
    
                scriptElements[url] = Loader.injectScriptElement(noCacheUrl, onLoad, onScriptError, scope);
            } else {
                if (typeof XMLHttpRequest != 'undefined') {
                    xhr = new XMLHttpRequest();
                } else {
                    xhr = new ActiveXObject('Microsoft.XMLHTTP');
                }
    
                try {
                    xhr.open('GET', noCacheUrl, false);
                    xhr.send(null);
                } catch (e) {
                    isCrossOriginRestricted = true;
                }
    
                status = (xhr.status === 1223) ? 204 :
                    (xhr.status === 0 && (self.location || {}).protocol == 'file:') ? 200 : xhr.status;
    
                isCrossOriginRestricted = isCrossOriginRestricted || (status === 0);
    
                if (isCrossOriginRestricted
                ) {
                }
                else if ((status >= 200 && status < 300) || (status === 304)
                ) {
                    // Debugger friendly, file names are still shown even though they're eval'ed code
                    // Breakpoints work on both Firebug and Chrome's Web Inspector
                    if (!Ext.isIE) {
                        debugSourceURL = "\n//@ sourceURL=" + url;
                    }
    
                    Ext.globalEval(xhr.responseText + debugSourceURL);
    
                    onLoad.call(scope);
                }
                else {
                }
    
                // Prevent potential IE memory leak
                xhr = null;
            }
        },
    

【问题讨论】:

  • xhr.open('GET', noCacheUrl, false); 删除false。但是,您必须将 try-catch 语句下方的所有内容移动到事件处理程序中,例如 xhr.addEventListener('load',function(){ ...code...})
  • @xufox 令人困惑,您可以在下面小提琴jsfiddle.net/e1hfjue1中的代码中更新@
  • 只有在使用动态加载时才会发生。这不是一个真正的问题。
  • jsfiddle.net/e1hfjue1/1 像这样的东西。我无法验证它是否真的有效,因为我没有完整的脚本。
  • @EvanTrimboli 但如何删除该警告

标签: javascript jquery html ajax extjs


【解决方案1】:

此警告仅在 ExtJs 开发环境的 Chrome 中显示。使用sencha cmd 构建应用程序后,警告将不再显示。正如@Evan 指出的那样,警告看起来是良性的,您不必担心。

存在警告这一事实不应成为问题,因为它永远不会显示在已部署的应用程序中。如果您还没有使用 sencha cmd,那么将它集成到您​​的开发周期中绝对值得。

我知道这不是问题的直接答案。我自己,我想知道是否有可能完全摆脱这个警告。

【讨论】:

  • 感谢您的回复...有没有我可以查看的演示以帮助我们更好地理解,,,
  • 对于 sencha cmd,演示很简单:下载它,sencha generate app,然后sencha app build,你就有了一个演示应用。
  • 你能分享一个工作简单、难以理解的小提琴吗:(
猜你喜欢
  • 1970-01-01
  • 2018-09-09
  • 1970-01-01
  • 1970-01-01
  • 2020-04-27
  • 2015-11-03
  • 2017-04-23
  • 2015-09-16
相关资源
最近更新 更多