Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.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 安格拉斯。添加全局AJAX错误处理程序(如果尚未定义)_Javascript_Ajax_Angularjs_Interceptor - Fatal编程技术网

Javascript 安格拉斯。添加全局AJAX错误处理程序(如果尚未定义)

Javascript 安格拉斯。添加全局AJAX错误处理程序(如果尚未定义),javascript,ajax,angularjs,interceptor,Javascript,Ajax,Angularjs,Interceptor,如何设置全局AJAX处理程序,只有在尚未为特定AJAX调用定义错误处理程序时才会调用该处理程序 如果发生错误,我的一些ajax调用确实需要执行一些逻辑(例如重新启用按钮),但对于一些ajax,我只需要在发生错误时显示错误消息 例如,这段代码没有为AJAX调用定义任何错误处理程序,因此我想应用于此调用全局错误处理程序,其中我将只显示错误消息: user.$delete().then(function () { // on success }); 但是这个AJAX调用已经定义了错误处理程序

如何设置全局AJAX处理程序,只有在尚未为特定AJAX调用定义错误处理程序时才会调用该处理程序

如果发生错误,我的一些ajax调用确实需要执行一些逻辑(例如重新启用按钮),但对于一些ajax,我只需要在发生错误时显示错误消息

例如,这段代码没有为AJAX调用定义任何错误处理程序,因此我想应用于此调用全局错误处理程序,其中我将只显示错误消息:

user.$delete().then(function () {
    // on success
});
但是这个AJAX调用已经定义了错误处理程序,我不想对其应用全局处理程序:

    $scope.deleteButtonEnabled = false;
    user.$delete().then(function () {
        // on success
    }, function(err) {   
        // Do some stuff and then show error message     
        $scope.deleteButtonEnabled = true;        
        alert('Error' + JSON.stringify(err))
    });
您可以在http调用中使用和一些配置

定义拦截器:-

angular.service('HttpInterceptor', function($q){
  var service = {
     response:function(response) { //Only if you need
        //If you have to handle response data based errors you could do this here
         return $q.when(response);
      },
     responseError: function(response){
        if(!response.config.suppressGlobal){ //Handle error only if the global error handling is not suppressed
            //Handle Error based
            alert('Error' + response.status);
        }

        return $q.reject(response);
    }
  };
}).config(['$httpProvider', function($httpProvider){
    $httpProvider.interceptors.push('HttpInterceptor'); //Push the interceptor here
}]);
在您的服务示例中,当您进行http或资源调用时,让该方法采用可选的布尔参数来抑制全局错误处理程序,并将其与http调用的
config
dictionary参数一起传递:-

   app.service('userService', function(){
        this.getData = function(suppressGlobal){ //Take a flag to suppress globals
             return  http.get('...', {suppressGlobal:suppressGlobal}).. //Pass the flag in the config
        };
        this.delete = function(suppressGlobal){ //Take a flag to suppress globals
          return http.delete('...', {suppressGlobal:suppressGlobal}).. //Pass the flag in the config
        }
        this.add = function(suppressGlobal){//Take a flag to suppress globals
            return http.post('..', data, {suppressGlobal:suppressGlobal}).. //Pass the flag in the config
        }
   });
当你打电话时:-

   // Code that needs global error handling
    user.delete().then(function () { //just call your service method as is
       // on success
    });
在其他地方:-

// Code that needs to suppress global error handling
user.delete(true).then(function () { //Pass true as argument to the delete which will supress global error handling.
    // on success
}, function(err) {   
    // Do some stuff and then show error message     
    $scope.deleteButtonEnabled = true;        
    alert('Error' + JSON.stringify(err))
});
您可以在http调用中使用和一些配置

定义拦截器:-

angular.service('HttpInterceptor', function($q){
  var service = {
     response:function(response) { //Only if you need
        //If you have to handle response data based errors you could do this here
         return $q.when(response);
      },
     responseError: function(response){
        if(!response.config.suppressGlobal){ //Handle error only if the global error handling is not suppressed
            //Handle Error based
            alert('Error' + response.status);
        }

        return $q.reject(response);
    }
  };
}).config(['$httpProvider', function($httpProvider){
    $httpProvider.interceptors.push('HttpInterceptor'); //Push the interceptor here
}]);
在您的服务示例中,当您进行http或资源调用时,让该方法采用可选的布尔参数来抑制全局错误处理程序,并将其与http调用的
config
dictionary参数一起传递:-

   app.service('userService', function(){
        this.getData = function(suppressGlobal){ //Take a flag to suppress globals
             return  http.get('...', {suppressGlobal:suppressGlobal}).. //Pass the flag in the config
        };
        this.delete = function(suppressGlobal){ //Take a flag to suppress globals
          return http.delete('...', {suppressGlobal:suppressGlobal}).. //Pass the flag in the config
        }
        this.add = function(suppressGlobal){//Take a flag to suppress globals
            return http.post('..', data, {suppressGlobal:suppressGlobal}).. //Pass the flag in the config
        }
   });
当你打电话时:-

   // Code that needs global error handling
    user.delete().then(function () { //just call your service method as is
       // on success
    });
在其他地方:-

// Code that needs to suppress global error handling
user.delete(true).then(function () { //Pass true as argument to the delete which will supress global error handling.
    // on success
}, function(err) {   
    // Do some stuff and then show error message     
    $scope.deleteButtonEnabled = true;        
    alert('Error' + JSON.stringify(err))
});

因此,最终您需要在每次失败时在一个地方显示错误消息,而不是在任何地方重复错误显示逻辑?@PSL。但是,如果我有一些不同的错误处理逻辑,我希望能够指定它并忽略此特定调用的全局处理程序。您是否使用angular http或其他东西?如果您使用angular http,您可以使用拦截器one@PSL对我想我会使用这个答案中描述的东西:但是我不知道如何检查当前拦截的请求是否已经有一个错误句柄。你能展示你的用户服务吗?您可以通过配置来处理它,因此最终您需要在每次失败时在一个地方显示错误消息,而不是在任何地方重复错误显示逻辑?@PSL。但是,如果我有一些不同的错误处理逻辑,我希望能够指定它并忽略此特定调用的全局处理程序。您是否使用angular http或其他东西?如果您使用angular http,您可以使用拦截器one@PSL对我想我会使用这个答案中描述的东西:但是我不知道如何检查当前拦截的请求是否已经有一个错误句柄。你能展示你的用户服务吗?您可以通过配置来处理它