Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Backbone.js global error handling

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.

like image 389
Eugene Manuilov Avatar asked Jul 26 '11 13:07

Eugene Manuilov


Video Answer


1 Answers

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

like image 164
sled Avatar answered Oct 19 '22 13:10

sled