从角度ui-router状态的$ http获取错误回调解决

[英]Getting the error callback from an $http get in an angular ui-router state resolve


I require some data from a website for a state to work, so I put an http request in resolve. However, if the request doesn't work, I can't seem to get any error message or anything to happen. The '$stateChangeError' callback doesn't work once a problem happens inside the $http callback. 'console.log' doesn't seems to print anything. It's as if the get request's failure callback never happens.

我需要一个网站的一些数据才能让一个州工作,所以我提出了一个http请求。但是,如果请求不起作用,我似乎无法收到任何错误消息或发生任何事情。一旦$ http回调内发生问题,'$ stateChangeError'回调就不起作用。 'console.log'似乎没有打印任何内容。这就好像get请求的失败回调永远不会发生。

resolve:{
    list-o-stuff: function($http, $rootScope){
        //throw 'test';
        //    ^that worked (it triggers '$stateChangeError')
        return $http.get('exampERROR!le.com').then( function(response){
            return response;
        }, function(response){
            console.log(response);
            //    that didn't do anything
            throw response;
            //    that didn't do anthing either
        });
    }
}

Bummer right. I guess I'll try adding another console.log and do a hard refresh... POOF! as soon as I ctrl+F5, firefox decides to show all that stuff that the '$stateChangeError' broadcast was supposed to do. If I use firebug, breakpoints in this callback are never triggered. Why? How can I debug like this?

对不起。我想我会尝试添加另一个console.log并进行硬刷新...... POOF!一旦我按Ctrl + F5,firefox决定显示'$ stateChangeError'广播应该做的所有东西。如果我使用firebug,则不会触发此回调中的断点。为什么?我该如何调试呢?

Thanks, Kenkron

1 个解决方案

#1


0  

Bear in mind that in order for the callback to be executed, the $http request must return an error code outside the range of 200 - 299.

请记住,为了执行回调,$ http请求必须返回200 - 299范围之外的错误代码。

According to the AngularJS documentation:

根据AngularJS文档:

"A response status code between 200 and 299 is considered a success status and will result in the success callback being called. Any response status code outside of that range is considered an error status and will result in the error callback being called. Also, status codes less than -1 are normalized to zero. -1 usually means the request was aborted, e.g. using a config.timeout. Note that if the response is a redirect, XMLHttpRequest will transparently follow it, meaning that the outcome (success or error) will be determined by the final response status code."

“200和299之间的响应状态代码被视为成功状态,将导致成功回调被调用。该范围之外的任何响应状态代码都被视为错误状态,并将导致调用错误回调。小于-1的代码归一化为零。-1通常意味着请求被中止,例如使用config.timeout。注意,如果响应是重定向,XMLHttpRequest将透明地跟随它,意味着结果(成功或错误)将由最终答复状态代码确定。“


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2016/08/06/b065c980640f2c3824b2a268a703282a.html



 
  © 2014-2022 ITdaan.com 联系我们: