I am writing website with usage of Backbone.js for web front end and own RESTful server for API back end. My API (RESTful) server requires manual authorization and expects security token in "Authorization" header. If security token is deprecated or broken, API server will return response with 401 status code. I override Backbone.sync to send additional headers with requests and returning options.error(jqXHR.status)
on AJAX error.
How can I add global error events listener to handle options.error(...)
events on failed resources loading? I need it to make navigation redirect to /#!/signin
page on 401 Unauthorized
response.
you could try to use the jQuery Global Ajax Event Handler:
http://api.jquery.com/category/ajax/global-ajax-event-handlers/
You simply bind the .ajaxError
event to the body or your application element and filter 401 status codes.
On a 401 error you redirect your application like window.yourAppRouter.navigate(...)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With