2015-09-11 48 views
1

這是我在Angular中處理我的路由的方式。我有幾個文件,其中存儲了有關每條路徑的所有必需數據,並且像這樣遍歷數據。 .html請求用於爲AJAX請求加載模板和.action。訪問標題數據並處理Angular http請求中的錯誤代碼

我需要做的是對待所有服務器不成功的情況(300,400,500等)。我搜索了所有我認爲相關的信息,發現了一些關於它的信息,但沒有任何相關或足夠清楚。我如何訪問請求的標題數據?

Application.config(['$routeProvider', '$interpolateProvider', '$httpProvider', 

    function($routeProvider, $interpolateProvider, $httpProvider) 
    { 

     $interpolateProvider.startSymbol('<%'); 
     $interpolateProvider.endSymbol('%>'); 

     for (var i=0; i < applicationRoutes.length; i++) { 

      if (applicationRoutes[i].hasInitialData) 
      { 
       $routeProvider.when('/' + applicationRoutes[i].path + '.html', { 

        templateUrl: applicationRoutes[i].path + '.html', 
        controller: applicationRoutes[i].controller + ' as ' + applicationRoutes[i].controllerAlias, 
        resolve: { 
         initData : ['Initialize', '$route', function(Initialize, $route) 
         { 
          //console.log($route.current.$$route); 
          return Initialize.serverData('/' + $route.current.$$route.pathBase); 
         }] 
        }, 
        pathBase: applicationRoutes[i].path 

       }).otherwise('/404'); 
      } 
      else 
      { 
       $routeProvider.when('/' + applicationRoutes[i].path + '.html', { 

        templateUrl: applicationRoutes[i].path + '.html', 
        controller: applicationRoutes[i].controller + ' as ' + applicationRoutes[i].controllerAlias, 
        pathBase: applicationRoutes[i].path 

       }).otherwise('/404'); 

      } 

     } 

     $httpProvider.defaults.headers.common["X-Requested-With"] = 'XMLHttpRequest'; 

    } 

]); 

回答

3

我不能看到你的服務器調用,但假設你已經推出了一個服務來調用端點與$ HTTP記住,你可以使用故障處理程序的承諾,以便

$http(req).then(function(){...}, function(){...}); 
的第二個參數

這意味着你可以做到這一點

$http(req).then(function(someData){/*This is the success*/},function(reason){/*the reason holds the reason it faild, reason.data etc */}); 
+0

你corrrect,但是我需要什麼,在當時是訪問由路由處理的請求頭的方式。我從那時開始設法處理http的問題 –

+0

您也可以使用'.catch(errorHandler)'代替'.then()'中的第二個回調函數。 – ste2425