Breeze js - 如何在查询响应中处理302重定向

时间:2013-06-10 14:09:38

标签: javascript asp.net-mvc breeze

如何处理由Breeze查询导致的错误,其中错误是由于XHR请求获得302重定向响应。

这是因为我的控制器受身份验证保护,当会话过期时,下一个请求被重定向到登录页面 - 返回HTTP 302响应,其中包含登录页面的位置标题。

当我检查promise所返回的错误对象时,XHR对象没有填充302代码或我可以用来识别错误的任何东西 - 我认为这是因为严格来说302不是错误

如何使用Breeze捕获此内容并将浏览器重定向到登录页面

1 个答案:

答案 0 :(得分:1)

在不知道实际返回给客户端的内容的情况下,这很难回答,但您可以通过提供自己的breeze ajax适配器拦截Breeze的ajax响应处理,然后在Breeze看到它之前自己处理原始的Http响应。类似的东西:

var origAjaxCtor = breeze.config.getAdapter("ajax");
var newAjaxCtor = function () {
    this.name = "newAjax";
    this._origAjaxCtor = new origAjaxCtor();
}
newAjaxCtor.prototype = new oldAjaxCtor(); // to delegate all other methods
newAjaxCtor.prototype.ajax = function (settings) {
   settings.success = function((data, textStatus, XHR) {
      // interpret the results yourself; augmenting them if needed
      ... { your code here } ...
      // and then call the original success code
      settings.success(data, textStatus, XHR);

   });
   // perform the actual ajax call - this will call your custom success method from    above.
   this._origAjaxCtor.ajax(settings);
}
// register the new adapter
breeze.config.registerAdapter("ajax", newAjaxCtor);
// make this adapter the default
breeze.config.initializeAdapterInstance("ajax", "newAjax", true);