1

In the end, I want to catch a 302 error and redirect to my login page, but right now xhr.status is getting a status code of 200.

Here's my current code:

parentSyncMethod = Backbone.sync
Backbone.sync = (method, model, options) ->
  old_error = options.old_error
  options.error = (xhr, text_status, error_thrown) ->
    if(xhr.status == 302)
      window.location.replace('http://localhost:8080/login')
    else
      old_error?(xhr, text_status, error_thrown)
  parentSyncMethod(method, model, options)

basically I think the problem is that the current webpage is throwing the 200 error, but the one that's throwing the 302 is wrapped and not propagating to the xhr.status. Is there a way to get all status code responses from all the get, post, put, etc. calls that have been made?

loganfsmyth
  • 156,129
  • 30
  • 331
  • 251
prashn64
  • 657
  • 1
  • 8
  • 24

1 Answers1

5

302 response codes are automatically redirected by the browser, so you will not be able to catch that response. Your handler will only run once data comes back from the URL that things were redirected to.

I'd say that instead of using a 302 here, you probably should just return 200 and have the response data signal a redirect in it's own way.

loganfsmyth
  • 156,129
  • 30
  • 331
  • 251
  • but there is not any browser catching this response, it is jQuery.ajax wich is caching it.. who is the one that is managing the redirect? – fguillen Apr 20 '12 at 16:12
  • @fguillen 302 redirects are handled transparently, so you will never see the response. jQuery calls the browser functions for making the request. When the response comes back, the browser will see the 302 status, look in the 'Location' header, and start a new request to that URL instead. When that request comes back, it will run your handler. – loganfsmyth Apr 20 '12 at 16:19