emberjs handle 401 not authorized

后端 未结 4 2079
情书的邮戳
情书的邮戳 2020-12-29 11:24

I am building an ember.js application and am hung up on authentication. The json rest backend is rails. Every request is authenticated using a session cookie (warden).

4条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2020-12-29 12:11

    As of the current version of Ember Data (1.0 beta) you can override the ajaxError method of DS.RESTAdapter:

    App.ApplicationAdapter = DS.RESTAdapter.extend({
      ajaxError: function(jqXHR) {
        var error = this._super(jqXHR);
        if (jqXHR && jqXHR.status === 401) {
          #handle the 401 error
        }
        return error;
      }
    });
    

    Note that you should call @_super, especially if you are overriding one of the more complex adapters like DS.ActiveModelAdapter, which handles 422 Unprocessable Entity.

提交回复
热议问题