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

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/

rzschech avatar Jul 31 '13 15:07 rzschech

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.

angadsalaria avatar Aug 07 '13 20:08 angadsalaria

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.

rzschech avatar Aug 07 '13 20:08 rzschech

+1 Currently we have to use jQuery.ajax to process unload event

ilplotkin avatar Nov 13 '13 08:11 ilplotkin

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).

IgorMinar avatar Jan 10 '14 08:01 IgorMinar

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).

4th-line avatar Jan 20 '14 00:01 4th-line

+1

ltamrazov avatar Feb 11 '14 13:02 ltamrazov

+1

leonidaz avatar Mar 06 '14 23:03 leonidaz

+1

poulp avatar Mar 17 '14 13:03 poulp

+1 Need synchronous behaviour in onbeforeunload

Magua avatar Apr 09 '14 21:04 Magua

Why weren't the commits merged into the master branch?

daviesgeek avatar Apr 14 '14 18:04 daviesgeek

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.

gx0r avatar Apr 14 '14 21:04 gx0r

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 :(.

caitp avatar Apr 14 '14 21:04 caitp

+1 for the @llambda reasons. Avoid pop-up blockers.

yokomizor avatar Apr 16 '14 16:04 yokomizor

+1 for the @llambda reasons.

redbar0n avatar Apr 19 '14 11:04 redbar0n

+1

maximkott avatar Apr 20 '14 10:04 maximkott

+1

rinatio avatar May 07 '14 11:05 rinatio

+1

schickling avatar May 15 '14 09:05 schickling

+1

aeternusrahl avatar Jun 23 '14 20:06 aeternusrahl

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

caitp avatar Jun 23 '14 20:06 caitp