You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Let's look at a code example. Imagine I need the value of foo to be added to all jQuery Ajax request headers in my app.
$.ajaxSetup({beforeSend: function(req){if(localStorage.foo){// Sync version - "foo" is locally availablereq.setRequestHeader('foo',localStorage.foo);}else{// Async version - "foo" needs to be fetched from an API serverreturn$.get({url: '/foo',beforeSend: null}).done(function(data){localStorage.foo=data;req.setRequestHeader('foo',data);});}}});
As the web is moving more and more to async operations, imho it would make sense to support this pattern in jQuery. Thoughts?
Thanks!
The text was updated successfully, but these errors were encountered:
pensierinmusica
changed the title
Allow jQuery.ajax.settings.beforeSend to return a promise
Allow jQuery.ajax.settings.beforeSend to handle async promises
Sep 18, 2015
This doesn't need to be done (and IMO shouldn't be done) inside$.ajax(), just create your memoizing function and call that. Using $.ajaxSetup() is definitely a bad idea unless you have written and understand every ajax call that will be made through these new semantics.
unless you have written and understand every ajax call that will be made through these new semantics.
@dmethvin, yep, that's exactly the case at hand. Anyways, this is what I ended up doing, although I wish jQuery Ajax could provide a better / async way to handle these situations:
Hi,
Currently
jQuery.ajax.settings.beforeSend
only works with sync code. It would be nice if it could support async code too.Let's look at a code example. Imagine I need the value of
foo
to be added to all jQuery Ajax request headers in my app.As the web is moving more and more to async operations, imho it would make sense to support this pattern in jQuery. Thoughts?
Thanks!
The text was updated successfully, but these errors were encountered: