Synchronous XMLHttpRequest deprecated

故事扮演 提交于 2019-12-18 08:40:57

问题


Today, I had to restart my browser due to some issue with an extension. What I found when I restarted it, was that my browser (Chromium) automatically updated to a new version that doesn't allow synchronous AJAX-requests anymore. Quote:

Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help, check http://xhr.spec.whatwg.org/.

I need synchronous AJAX-requests for my node.js applications to work though, as they store and load data from disk through a server utilizing fopen. I found this to be a very simplistic and effective way of doing things, very handy in the creation of little hobby projects and editors... Is there a way to re-enable synchronous XMLHttpRequests in Chrome/Chromium?


回答1:


This answer has been edited.

Short answer: They don't want sync on the main thread.

The solution is simple for new browsers that support threads/web workers:

var foo = new Worker("scriptWithSyncRequests.js")

Neither DOM nor global vairables aren't going to be visible within a worker but encapsulation of multiple synchronous requests is going to be really easy.

Alternative solution is to switch to async but to use browser localStorage along with JSON.stringify as a medium. You might be able to mock localStorage if you allowed to do some IO. http://caniuse.com/#search=localstorage

Just for fun, there are alternative hacks if we want to restrict our self using only sync:

It is tempting to use setTimeout because one might think it is a good way to encapsulate synchronous requests together. Sadly, there is a gotcha. Async in javascript doesn't mean it gets to run in its own thread. Async is likely postponing the call, waiting for others to finish. Lucky for us there is light at the end of the tunnel because it is likely you can use xhttp.timeout along with xhttp.ontimeout to recover. See Timeout XMLHttpRequest This means we can implement tiny version of a schedular that handles failed request and allocates time to try again or report error.

// The basic idea.
function runSchedular(s)
{
    setTimeout(function() {
        if (s.ptr < callQueue.length) {
            // Handles rescheduling if needed by pushing the que.
            // Remember to set time for xhttp.timeout.
            // Use xhttp.ontimeout to set default return value for failure.
            // The pushed function might do something like: (in pesudo)
            // if !d1
            // d1 = get(http...?query);
            // if !d2
            // d2 = get(http...?query);
            // if (!d1) {pushQue tryAgainLater}
            // if (!d2) {pushQue tryAgainLater}
            // if (d1 && d2) {pushQue handleData}
            s = s.callQueue[s.ptr++](s);
        } else {
            // Clear the que when there is nothing more to do.
            s.ptr = 0;
            s.callQueue = [];
            // You could implement an idle counter and increase this value to free
            // CPU time.
            s.t = 200;
        }
        runSchedular(s);
    }, s.t);
}



回答2:


Doesn't "deprecated" mean that it's available, but won't be forever. (I read elsewhere that it won't be going away for a number of years.) If so, and this is for hobby projects, then perhaps you could use async: false for now as a quick way to get the job done?



来源:https://stackoverflow.com/questions/29315005/synchronous-xmlhttprequest-deprecated

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!