[英]ionic http request loading timeout
每当执行http请求时,我都会使用以下内容显示加载屏幕,但是有时如果出现错误,它将保持加载状态(由于该应用无法使用,因此会在后台显示)。 而不是在每个错误检查器上都隐藏它,我想知道是否可以在5秒后调用超时?
.config(function($httpProvider) {
$httpProvider.defaults.timeout = 5000;
$httpProvider.interceptors.push(function($rootScope) {
return {
request: function(config) {
$rootScope.$broadcast('loading:show')
return config
},
response: function(response) {
$rootScope.$broadcast('loading:hide')
return response
}
}
})
})
遵循Jess的回答,现在看起来像这样:
.config(function($httpProvider) {
$httpProvider.defaults.timeout = 5000;
$httpProvider.interceptors.push(function($rootScope) {
return {
request: function(config) {
$rootScope.$broadcast('loading:show')
return config
},
response: function(response) {
$rootScope.$broadcast('loading:hide')
return response
},
responseError: function(response) {
$rootScope.$broadcast('loading:hide')
return response
},
requestError: function(response) {
$rootScope.$broadcast('loading:hide')
return response
}
}
})
})
但是,我似乎无法在requestError
放置警报以通知用户。
问题如何实施警报以通知用户已发生的错误?
尝试添加responseError
和requestError
,如下所示:
responseError: function(responseError) {
$rootScope.$broadcast('loading:hide')
return responseError
然后使用requestErro
r再次执行此操作,这是从有角度的http拦截器文档中获得的
requestError
:当先前的拦截器抛出错误或被拒绝解决时,拦截器将被调用。
responseError
:前一个拦截器抛出错误或被拒绝解决时,拦截器将被调用。
编辑以回答评论:
因此,如果您想对responseError
发出警报, responseError
不是添加$rootScope.$broadcast('response:error')
在responseError函数中
然后在控制器中您要发出警报,只需执行
$scope.$on('response:error', function(){throw the error here});
您也可以对requestError
做同样的事情
之所以有效,是因为$broadcast
将事件向下调度到所有子范围
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.