Synchronous XMLHttpRequest warning and [removed]

前端 未结 7 1903
猫巷女王i
猫巷女王i 2020-11-27 16:56

I\'m getting a warning message:

Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user\'s expe

相关标签:
7条回答
  • 2020-11-27 17:20

    Browsers now warn for the use of synchronous XHR. MDN says this was implemented recently:

    Starting with Gecko 30.0 (Firefox 30.0 / Thunderbird 30.0 / SeaMonkey 2.27)

    https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Synchronous_and_Asynchronous_Requests#Synchronous_request

    Here's how the change got implemented in Firefox and Chromium:

    • https://bugzilla.mozilla.org/show_bug.cgi?id=969671
    • http://src.chromium.org/viewvc/blink?view=revision&revision=184788

    As for Chrome people report this started happening somewhere around version 39. I'm not sure how to link a revision/changeset to a particular version of Chrome.

    Yes, it happens when jQuery appends markup to the page including script tags that load external js files. You can reproduce it with something like this:

    $('body').append('<script src="foo.js"></script>');
    

    I guess jQuery will fix this in some future version. Until then we can either ignore it or use A. Wolff's suggestion above.

    0 讨论(0)
  • 2020-11-27 17:23

    Even the latest jQuery has that line, so you have these options:

    • Change the source of jQuery yourself - but maybe there is a good reason for its usage
    • Live with the warning, please note that this option is deprecated and not obsolete.
    • Change your code, so it does not use this function

    I think number 2 is the most sensible course of action in this case.

    By the way if you haven't already tried, try this out: $.ajaxSetup({async:true});, but I don't think it will work.

    0 讨论(0)
  • 2020-11-27 17:23

    I was plagued by this error message despite using async: true. It turns out the actual problem was using the success method. I changed this to done and warning is gone.

    success: function(response) { ... }

    replaced with:

    done: function(response) { ... }

    0 讨论(0)
  • 2020-11-27 17:30

    UPDATE: This has been fixed in jQuery 3.x. If you have no possibility to upgrade to any version above 3.0, you could use following snippet BUT be aware that now you will lose sync behaviour of script loading in the targeted content.

    You could fix it, setting explicitly async option of xhr request to true:

    $.ajaxPrefilter(function( options, original_Options, jqXHR ) {
        options.async = true;
    });
    
    0 讨论(0)
  • 2020-11-27 17:30

    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

    0 讨论(0)
  • 2020-11-27 17:34

    In my case if i append script tag like this :

    var script = document.createElement('script');
    script.src = 'url/test.js';
    $('head').append($(script));
    

    i get that warning but if i append script tag to head first then change src warning gone !

    var script = document.createElement('script');
    $('head').append($(script));
    script.src = 'url/test.js';
    

    works fine!!

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