Search code examples
asp.netbreeze

BreezeJS intercept server response


Due to an issue with ASP.NET Identity 2.0, the server returns HTTP 200 with a message saying HTTP 400 and I am trying to intercept it in order to redirect the user to the login page. This is the code I have but it seems that the site does not progress any longer. I understand that the issue is resolved in ASP.NET Identity 3.0 but it is not an option right now. Any ideas on the code below?

var ajaxAdapter = breeze.config.getAdapterInstance('ajax');

ajaxAdapter.requestInterceptor = function (requestInfo) {
requestInfo.success = function (response) {
    // process response message here.
    return response;
}};

Solution

  • I ended up fixing this by assigning the original success function to a variable and then calling it after my own code:

    var ajaxAdapter = breeze.config.getAdapterInstance('ajax');
    
    ajaxAdapter.requestInterceptor = function (requestInfo) {
    var oldSuccessFn = requestInfo.success;
    
    requestInfo.success = function (data, statusText, jqXHR) {
        if (data.Message == "Authorization has been denied for this request.") {
            signOut();
        } else {
            var result;
            oldSuccessFn.call(result, data, statusText, jqXHR);
            return result;
        }
    },