JavaScript console.log causes error: “Synchronous XMLHttpRequest on the main thread is deprecated…”

后端 未结 21 1679
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-22 09:26

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

相关标签:
21条回答
  • 2020-11-22 09:45

    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/

    0 讨论(0)
  • 2020-11-22 09:47

    The warning message MAY BE due to an XMLHttpRequest request within the main thread with the async flag set to false.

    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. (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. User agents are strongly encouraged to warn about such usage in developer tools and may experiment with throwing an InvalidAccessError exception when it occurs.

    The future direction is to only allow XMLHttpRequests in worker threads. The message is intended to be a warning to that effect.

    0 讨论(0)
  • 2020-11-22 09:47

    Sometimes it's necessary to ajax load a script but delay document ready until after the script is loaded.

    jQuery supports this with the holdReady() function.

    Example usage:

    $.holdReady(true);                              //set hold
    function releaseHold() { $.holdReady(false); }  //callback to release hold
    $.getScript('script.js', releaseHold);          //load script then release hold
    

    The actual script loading is asynchronous (no error), but the effect is synchronous if the rest of your JavaScript runs after document ready.

    This advanced feature would typically be used by dynamic script loaders that want to load additional JavaScript such as jQuery plugins before allowing the ready event to occur, even though the DOM may be ready.

    Documentation:
    https://api.jquery.com/jquery.holdready


    UPDATE January 7, 2019

    From 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.

    0 讨论(0)
  • 2020-11-22 09:49

    In my case this was caused by the flexie script which was part of the "CDNJS Selections" app offered by Cloudflare.

    According to Cloudflare "This app is being deprecated in March 2015". 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

    NB: this is a copy of my answer on this thread Synchronous XMLHttpRequest warning and <script> (I visited both when looking for a solution)

    0 讨论(0)
  • 2020-11-22 09:51

    For me, the problem was that in an OK request, I expected the ajax response to be a well formatted HTML string such as a table, but in this case, the server was experiencing a problem with the request, redirecting to an error page, and was therefore returning back the HTML code of the error page (which had a <script tag somewhere. I console logged the ajax response and that's when I realized it was not what I was expecting, then proceeded to do my debugging.

    0 讨论(0)
  • 2020-11-22 09:52

    This is solved in my case.

    JS

    $.ajaxPrefilter(function( options, original_Options, jqXHR ) {
        options.async = true;
    });
    

    This answer was inserted in this link

    https://stackoverflow.com/questions/28322636/synchronous-xmlhttprequest-warning-and-script

    0 讨论(0)
提交回复
热议问题