简体   繁体   English

ionic http请求加载超时

[英]ionic http request loading timeout

I am using the following to show a loading screen whenever I am performing a http request however sometimes if there is an error then it will stay loading (because of the backdrop the app becomes unusable). 每当执行http请求时,我都会使用以下内容显示加载屏幕,但是有时如果出现错误,它将保持加载状态(由于该应用无法使用,因此会在后台显示)。 Rather than hide it on every error checker I was wondering if it is possible to call the timeout after 5 seconds? 而不是在每个错误检查器上都隐藏它,我想知道是否可以在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
            }
        }
    })
})

Following Jess's answer it now looks like this : 遵循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
            }
        }
    })
})

However I cannot seem to be able to put an alert in the requestError to inform the user. 但是,我似乎无法在requestError放置警报以通知用户。

Question How can I implement an alert to notify the user of the error that has occurred? 问题如何实施警报以通知用户已发生的错误?

try adding responseError and requestError so like this: 尝试添加responseErrorrequestError ,如下所示:

 responseError: function(responseError) {
                $rootScope.$broadcast('loading:hide')
                return responseError

and do this again with requestErro r, This is from the angular http interceptors docs 然后使用requestErro r再次执行此操作,这是从有角度的http拦截器文档中获得的

requestError : interceptor gets called when a previous interceptor threw an error or resolved with a rejection. requestError :当先前的拦截器抛出错误或被拒绝解决时,拦截器将被调用。

responseError : interceptor gets called when a previous interceptor threw an error or resolved with a rejection. responseError :前一个拦截器抛出错误或被拒绝解决时,拦截器将被调用。

Edit to answer comment: 编辑以回答评论:

so if you want to throw a alert on responseError than a add a $rootScope.$broadcast('response:error') 因此,如果您想对responseError发出警报, responseError不是添加$rootScope.$broadcast('response:error')

in the responseError function 在responseError函数中

then in the controller you want to throw the alert in just do a 然后在控制器中您要发出警报,只需执行

$scope.$on('response:error', function(){throw the error here});

you can also do the same for requestError 您也可以对requestError做同样的事情

this works because $broadcast -- dispatches the event downwards to all child scopes 之所以有效,是因为$broadcast将事件向下调度到所有子范围

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM