Eugene Manuilov
Eugene Manuilov

Reputation: 4361

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.

Upvotes: 12

Views: 5673

Answers (2)

Henry Wilson
Henry Wilson

Reputation: 3351

In a similar vein to the suggestion of Global Ajax Event Handler, you can handle this via the jQuery ajaxSetup method: https://stackoverflow.com/a/19981363/886153

Upvotes: 0

sled
sled

Reputation: 14635

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

Upvotes: 10

Related Questions