angular.js icon indicating copy to clipboard operation
angular.js copied to clipboard

Add an option to make synchronous XHR requests

Open samuelreh opened this issue 13 years ago • 30 comments

Any plans on adding the ability to create synchronous XHR requests? Would be nice for unloadpage events.

samuelreh avatar Apr 12 '13 21:04 samuelreh

https://www.w3.org/Bugs/Public/show_bug.cgi?id=24790 seriously, no

caitp avatar Aug 29 '14 05:08 caitp

+1

Logrythmik avatar Nov 11 '14 20:11 Logrythmik

https://www.w3.org/Bugs/Public/show_bug.cgi?id=24790 seriously, no

@Logrythmik we aren't going to add support for this, we really want to pull support for synchronous XHR out of browsers entirely. It's not a thing we want to have, so we sure aren't going to encourage it from angular. It's a bad user experience, period :(

See https://xhr.spec.whatwg.org/#sync-warning, too

caitp avatar Nov 11 '14 20:11 caitp

Given that certain browsers are simply depreciating sync xhr requests:

Note: Starting with Gecko 30.0 (Firefox 30.0 / Thunderbird 30.0 / SeaMonkey 2.27), synchronous requests on the main thread have been deprecated due to the negative effects to the user experience.

https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest I really don't think we should be considering adding it to $http.

pkozlowski-opensource avatar Nov 11 '14 20:11 pkozlowski-opensource

Oh, @caitp was 26 seconds faster :-)

pkozlowski-opensource avatar Nov 11 '14 20:11 pkozlowski-opensource

Browsers have marked the feature depreciated, but they don't intend on removing the feature. The depreciation is just to encourage users to use async request. Sync is still needed for beforeunload.

JemiloII avatar Sep 01 '15 20:09 JemiloII

Great... So, the sum of all the above is that you can't save data on your server on unload / beforeunload, e.g. on window-close. Another giant leap forward for the web...

DumboJet avatar Nov 11 '15 16:11 DumboJet

You can, just use raw javascript, I can't imagine that you'd want to do a million unload request.

JemiloII avatar Dec 08 '15 16:12 JemiloII

Yes, I know, but when using angular you will have to make non-Angular repositories for the same repository actions and that is code duplication (and it creates a big mess in my case). Also, the word depreciated is not well defined for browser features, I think. So, surprises may come in the future.

I have chosen to inform the user that if he leaves the page he will loose data, but I am not into JS alert messages so much and the way my app works makes the alert seem like a stupid idea.

But what can we do? That's the API.

DumboJet avatar Dec 09 '15 06:12 DumboJet

A very workaround-y workaround would be to use a custom $xhrFactory, that is able to create sync requests.

The $xhrFactory is passed the method and url for the request and must return an XMLHttpRequest instance (let's call it xhr). One could overwrite xhr.open() to make synchronous requests. The problem is that the only info available to the xhrFactory, in order to decide if it's a sync or async request, are method and url. One possible hack to overcome this, is to use a special prefix on the method, which will be stripped by the overwritten open() method (e.g. use sync-get instead of get for making a synchronous get request).

Something like this:

.factory('$xhrFactory', function xhrFactoryWithSyncSupport($window) {
  var isSyncRegex = /^SYNC-/i;

  return createXhr;

  function createXhr(method, url) {
    var isSync = isSyncRegex.test(method);
    var xhr = new $window.XMLHttpRequest();

    if (isSync) {
      // Let's overwrite `open()` to make a sync request
      var originalOpen = xhr.open;
      xhr.open = function openSync(method, url) {
        method = method.replace(isSyncRegex, '');
        return originalOpen.call(xhr, method, url, false);
      };
    }

    return xhr;
  }

You can see it in action here.

gkalpak avatar Dec 10 '15 11:12 gkalpak