如何检测所有 http GET 失败
How to detect all http GET fails?
在我的应用程序中,我有一个网格,它通过HTTP承诺获取数据。 目前我可以检测到errorCallback
s - 如下所示:
myDataPromise.then(function () {
//do stuff
}, function errorCallback(response) {
if (response.statusText === "Not Found") {
//do stuff
}else if(response.statusText === "Internal Server Error"){
//do stuff
}
但是对于SSL错误,chrome会回击"ERR::CONNECTION被拒绝",我无法像404等那样通过读取字符串来了解这一点。 我真正想要的是显示一个简单的图像/文本,说明检索数据时出现错误,无论它是什么。 因此,如果 http GET 完全失败 - 用户知道。 在我看来,这是一个相当典型的规格,但我在网上找不到太多关于它的信息。
你必须创建一个拦截器
// 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');
// alternatively, register the interceptor via an anonymous factory
$httpProvider.interceptors.push(function($q, dependency1, dependency2) {
return {
'request': function(config) {
// same as above
},
'response': function(response) {
// same as above
}
};
});
拦截器拦截所有$http流量。即使在加载模板时也是如此。
相关文章:
- 如何将PHP get查询转换为Meteor's HTTP.get()
- Angularjs http.get() Cross-Origin 请求被阻止
- Angular js http get返回所有页面
- Angular.js-$http.get-使用文件中的缓存
- 当我在chrome中切换设备模式时,$http.get请求失败
- Angular http.get没有'我不喜欢在开头和结尾加方括号的文本文件
- 从javaspring方法管理http.get响应
- 我可以在React应用程序中使用什么作为Angular的等价物;s$http.get
- 如何让foreach循环等待$http.get完成后再继续
- Angular Cross Origin Request CORS失败,但节点http.get()成功返回
- 使用$http.get()-OAuth身份验证下载带有Angular的远程文件
- $http GET URL更改并查找错误的资源
- angularJS $http.get communicating with API
- 在AngularJS中使用$http.get检索JSON时出现错误400
- AngularJS $http GET 请求到本地 JSON 文件返回语法错误:意外令牌]
- 使用角度$http.get从服务器下载文件
- AngularJS或JQuery解析错误格式的JSON(http get)
- AngularJS HTTP Get 没有得到响应
- 无法在角度 js 中将参数传递给 http get 请求
- Meteor:使用HTTP.get从客户端访问网站(CORS错误)