问题
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?
回答1:
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.
来源:https://stackoverflow.com/questions/10249310/backbone-sync-get-all-response-error-status-codes