Add an option to make synchronous XHR requests
Any plans on adding the ability to create synchronous XHR requests? Would be nice for unloadpage events.
https://www.w3.org/Bugs/Public/show_bug.cgi?id=24790 seriously, no
+1
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
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.
Oh, @caitp was 26 seconds faster :-)
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.
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...
You can, just use raw javascript, I can't imagine that you'd want to do a million unload request.
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.
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.