backbone.sync get all response error status codes

柔情痞子 提交于 2020-01-16 08:06:10

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!