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.
Agreed.
I would like to clean up some server side state when the page is unloaded. To implement this I need to ensure a HTTP request reaches the server before the HTTP request is aborted by the page unload process. This requires synchronous HTTP requests. See:
https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Synchronous_and_Asynchronous_Requests#Irreplaceability_of_the_synchronous_use
Also JQuery has a setting for specifying async behavior:
http://api.jquery.com/jQuery.ajax/
Synchronous behavior can be achieved using promise and deferred objects in the $q service provided by angularjs. The $http service returns a promise object to help with that.
However, I agree that having a straightforward 'synchronous request' feature in $http service would be a helpful option.
Except that the synchronous behavior of promises and deferred objects does not complete because the page unload process interrupts it. The synchronous XHR blocks the page unload process from completing until the XHR is complete. See the MDN reference above which describes why the synchronous request is irreplaceable.
+1 Currently we have to use jQuery.ajax to process unload event
This onunload thing is an interesting use-case that we haven't considered. If it weren't for that, I'd close this issue as won't fix.
@rzschech's commit doesn't have a PR associated with it AFAICT. It also lacks documentation and tests. We should also ensure that the sync nature doesn't break the promise model (I don't think it should because promises will turn sync resolution into async automatically).
Hi, it would also help for popup.
My usecase : get a short URL with bit.ly API and open a popup (twitter, fb, g+,...) to share the link. The popup will not open if inside a callback, it must be directly linked to a user action (a click for instance).
+1
+1
+1
+1 Need synchronous behaviour in onbeforeunload
Why weren't the commits merged into the master branch?
Also if you have a <a ng-click="someFunction()">, and someFunction opens a link in a new window (or new tab), it is necessary to use a synchronous XHR inside that function, otherwise browsers block it as a pop up.
Maybe synchronous XHR doesn't fit in nicely with Angular's $http, for the reasons Igor mentioned. Other concerns have been raised about it blocking the flow of the app which probably shouldn't be encouraged.
I think if you were brave, you could write a decorator for $httpBackend which would take the 'sync' flag into consideration, but it might be a bit tricky under the current model. But basically if you do need synchronous XHR, nothing is really stopping you from creating the XHR object yourself, you'd just need to be aware of all the silly cross-browser quirks that come with XHR.
So, I realize we could make this a bit nicer for people (and maybe we could improve $httpBackend further by separating XHR and JSONP requests into separate services so that they're easier to decorate), but it seems like we probably aren't going to land this at this time :(.
+1 for the @llambda reasons. Avoid pop-up blockers.
+1 for the @llambda reasons.
+1
+1
+1
+1
guys, you know anne van kesteren (among others) is talking about deprecating synchronous XHR, I don't think we want to make it easier for people to use --- fewer apps using it means it's easier for implementations to remove it once we don't need it anymore