從角度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 联系我们: