Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/474.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript $httpProvider.responseInterceptors的替代方案_Javascript_Angularjs_Interceptor - Fatal编程技术网

Javascript $httpProvider.responseInterceptors的替代方案

Javascript $httpProvider.responseInterceptors的替代方案,javascript,angularjs,interceptor,Javascript,Angularjs,Interceptor,$httpProvider.responseInterceptors在AngularJS V1.3中停止使用时的替代方案是什么 我的拦截器以前使用AngularJS1.2,现在不使用1.3版 var angularErrorHandling = angular.module('xx-http-error-handling', []); angularErrorHandling.config(function ($provide, $httpProvider, $compileProvider)

$httpProvider.responseInterceptors在AngularJS V1.3中停止使用时的替代方案是什么

我的拦截器以前使用AngularJS1.2,现在不使用1.3版

var angularErrorHandling = angular.module('xx-http-error-handling', []);
angularErrorHandling.config(function ($provide, $httpProvider, $compileProvider) {
    var elementsList = $();

     push function to the responseInterceptors which will intercept 
     the http responses of the whole application
    $httpProvider.responseInterceptors.push(function ($timeout, $q) {
        return function (promise) {
            return promise.then(function (successResponse) {                
                // if there is a successful response on POST, UPDATE or DELETE we display
                // a success message with green background
                if (successResponse.config.method.toUpperCase() == 'GET') {
                    var length = successResponse.data.length;                    
                    if (length == 0)
                    {
                        var countactivetoaster = $('#toast-container').find('.toast').length;
                        if (countactivetoaster == 0) {
                            toastr.warning('No Records Found!', '');
                        }
                    }                    
                    return successResponse;
                }
                else if (successResponse.config.method.toUpperCase() == 'PUT') {                   
                    toastr.success('Data Saved Sucessfully..', '');
                    return successResponse;
                }
                else if (successResponse.config.method.toUpperCase() == 'POST') {                   
                    toastr.success('Data Saved Sucessfully..', '');
                    return successResponse;
                }
            },
            // if the message returns unsuccessful we display the error 
            function (errorResponse) {
                switch (errorResponse.status) {
                    case 400: // if the status is 400 we return the error                           
                        toastr.error('400 error.', '');
                        // if we have found validation error messages we will loop through
                        // and display them
                        if (errorResponse.data.errors.length > 0) {
                            for (var i = 0; i < errorResponse.data.errors.length; i++) {
                                toastr.error('xx-http-error-validation-message', '');
                            }
                        }
                        break;
                    case 401: // if the status is 401 we return access denied                            
                        toastr.error('Wrong email address or password!', '');

                        break;
                    case 403: // if the status is 403 we tell the user that authorization was denied                          
                        toastr.error('You have insufficient privileges to do what you want to do!', '');
                        break;
                    case 500: // if the status is 500 we return an internal server error message                            
                        toastr.error('Error: <br />' +
                            errorResponse.data.exceptionMessage != null && errorResponse.data.exceptionMessage.length > 0 ? errorResponse.data.exceptionMessage : 
                            errorResponse.data.message, '');
                        break;
                    default: // for all other errors we display a default error message                            
                        toastr.error('Error ' + errorResponse.status + ': ' + errorResponse.data.message, '');
                }
                return $q.reject(errorResponse);
            });
        };
    });

    $compileProvider.directive('httpErrorMessages', function () {
        return {
            link: function (scope, element, attrs) {
                elementsList.push($(element));
            }
        };
    });
});
var angularrorhandling=angular.module('xx-http-error-handling',[]);
angularErrorHandling.config(函数($provide、$httpProvider、$compileProvider){
var elementsList=$();
将功能推送到将进行拦截的响应接收器
整个应用程序的http响应
$httpProvider.responseInterceptors.push(函数($timeout,$q){
返回函数(承诺){
return promise.then(函数(successResponse){
//如果在POST上有成功的响应,我们将显示更新或删除
//绿色背景的成功消息
if(successResponse.config.method.toUpperCase()=='GET'){
var length=successResponse.data.length;
如果(长度==0)
{
var countactivetoaster=$(“#toast container”).find(“.toast”).length;
如果(countactivetoaster==0){
toastr.warning('未找到记录!','');
}
}                    
返回成功响应;
}
else if(successResponse.config.method.toUpperCase()=='PUT'){
toastr.success('成功保存数据…','');
返回成功响应;
}
else if(successResponse.config.method.toUpperCase()=='POST'){
toastr.success('成功保存数据…','');
返回成功响应;
}
},
//如果消息返回不成功,我们将显示错误
功能(错误响应){
开关(errorResponse.status){
案例400://如果状态为400,则返回错误
toastr.错误('400错误','');
//如果我们发现了验证错误消息,我们将进行循环
//并展示它们
if(errorResponse.data.errors.length>0){
对于(var i=0;i'+
errorResponse.data.exceptionMessage!=null&&errorResponse.data.exceptionMessage.length>0?errorResponse.data.exceptionMessage:
errorResponse.data.message“”);
打破
default://对于所有其他错误,我们显示一条默认错误消息
toastr.error('error'+errorResponse.status+':'+errorResponse.data.message',);
}
返回$q.reject(错误响应);
});
};
});
$compileProvider.directive('httpErrorMessages',函数(){
返回{
链接:函数(范围、元素、属性){
elementsList.push($(element));
}
};
});
});

您必须使用新的拦截器语法(在我看来更简洁/更好):

现在您将看到,您可以分别处理4个拦截器:request、requestError、response、responseError

// register the interceptor as a service
  $provide.factory('myHttpInterceptor', function($q, dependency1, dependency2) {
    return {
      // optional method
      'request': function(config) {
        // do something on success
        return config;
      },

      // optional method
     'requestError': function(rejection) {
        // do something on error
        if (canRecover(rejection)) {
          return responseOrNewPromise
        }
        return $q.reject(rejection);
      },



      // optional method
      'response': function(response) {
        // do something on success
        return response;
      },

      // optional method
     'responseError': function(rejection) {
        // do something on error
        if (canRecover(rejection)) {
          return responseOrNewPromise
        }
        return $q.reject(rejection);
      }
    };
  });

  $httpProvider.interceptors.push('myHttpInterceptor');

更多信息:(拦截器章节)

以及关于原因和解决方案-我得到错误:未捕获引用错误:$provider未定义我给了您angularJS文档示例。。查找/修复您的错误或编辑您的帖子以显示新代码。当我用我的应用程序对象替换$provider时,它对我很有效。