处理AngularJS中的HTTP 500内部服务器错误
我正在用ionic构建一个应用程序,我想处理$http()AJAX调用中发生的500内部服务器错误。有可能创建任何拦截器来处理这个问题吗 这是我的控制器代码:处理AngularJS中的HTTP 500内部服务器错误,angularjs,ajax,ionic-framework,Angularjs,Ajax,Ionic Framework,我正在用ionic构建一个应用程序,我想处理$http()AJAX调用中发生的500内部服务器错误。有可能创建任何拦截器来处理这个问题吗 这是我的控制器代码: .controller('RegistrationController',function($scope,$http){ $scope.doRegistration = function(){ $http({ method : "POST",
.controller('RegistrationController',function($scope,$http){
$scope.doRegistration = function(){
$http({
method : "POST",
url : BASE_URL,
params : {
//sending the parameters as JSON
}
}).then(function successCallback(response){
if(response.status == 200)
//handling the response sent by server
},function errorCallback()});
};
});
如果用户输入了一些无效的输入,而服务器无法处理,则会抛出500个内部服务器错误。当进行AJAX调用时,我会显示一个微调器,当出现一些服务器错误时,我应该停止AJAX调用并停止微调器,并向用户显示一些弹出窗口,说“遇到服务器错误”。您可以如下创建拦截器-
app.factory('testResponseInterceptor', function ($q ,$rootScope,$location) {
return {
response: function (response) {
//correct response
return response;
},
responseError: function (response) {
//error in response
}
};
});
并在您的模块中进行配置-
app.config(function ($httpProvider) {
$httpProvider.interceptors.push('testResponseInterceptor');
});
您可以创建拦截器,如下所示-
app.factory('testResponseInterceptor', function ($q ,$rootScope,$location) {
return {
response: function (response) {
//correct response
return response;
},
responseError: function (response) {
//error in response
}
};
});
并在您的模块中进行配置-
app.config(function ($httpProvider) {
$httpProvider.interceptors.push('testResponseInterceptor');
});