簡體   English   中英

Angular服務未返回錯誤響應

[英]Angular service is not returning error response

我正在調用service saveRouteInfo() ,但是僅成功回調被調用以用於控制器中的錯誤和成功。

服務

gisApp.factory('rpServc', function($http) {
      return {
          saveRouteInfo:function(routeInfo){
              return $http.post("/GISWebApp/gis/api/v1/route",routeInfo).then(
                  function(response) 
                  {  
                      return response.data;           
                  }, function(response) {
                      return response.data;
                  });   
            },
      }; 
});

調節器

rpServc.saveRouteInfo(routeInfo).then(function(data) {
    alert(JSON.stringify(data));

}, function(error) {
    alert(JSON.stringify(error));
});

使用$ q

 gisApp.factory('rpServc', function($http, $q) {
        var deferred = $q.defer();
        return {
            saveRouteInfo:function(routeInfo){
                return $http.post("/GISWebApp/gis/api/v1/route", routeInfo).
                then(function(response) {
                  return response;          
                }, function(reason) {
                  return $q.reject(reason);
                });
            },
        };
    });

您應該閱讀$ q角度承諾文檔

並返回已解決或已拒絕的諾言,因此您可以在.then()第二個函數中捕獲被拒絕的諾言。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM