angularjs如何正确处理$http调用的错误?

eaf3rand  于 2021-09-29  发布在  Java
关注(0)|答案(1)|浏览(308)

我的控制器正在变空或关闭 undefined 从服务传回的数据,这反过来导致控制器中出现无法处理的错误 undefined 结果。我正在重读有关错误处理的资料,开始怀疑我的错误处理是否不完整。因为我的控制器正在得到响应,但是响应是空的/未定义的,那么这意味着我的服务仍然在传回错误的响应……但是基于我的服务,我认为应该使用 catch . 我如何解决这个问题,使我的服务处理错误,只向控制器传回正确的数据?
控制器:

.controller('listCtrl', function($scope,listService) {

listService.getList(1)
.then(function(res) {
  if (res.list!= null) {   // <--- `Exception: Cannot read property 'list' of undefined`
     ...
     ...
  } else {
     ...
     ...
  }
}) ;

服务:

.factory("listService", function($http,$q) {
  // errMgmt 5100

  var headers={} ;
  var listMain = [] ;  // Current Lists
  var listPast = [] ;  // Past Lists

  function setVars() {
    baseUrl = "https://api.mydomain.com/v1/index.php" ;
    headers = {
      'Pragma':'no-cache', 
      'Expires': -1, 
      'Cache-Control':'no-cache,no-store,must-revalidate', 
      'Content-Type' : 'application/json',        
      'X-Requested-With':'com.mydomain',
      Authorization: 'Token ' +clientToken
    } ;
  }

  function getList(typeID) {
    if (typeID == 1) {
      listMain = [] ;
    } else if (typeID == 2) {
      listPast = [] ;
    }
    setVars() ;
    var dataObj = [{"type":typeID,"userID":userData.user_ID}] ;
    var req = {
      method: 'POST',
      url: baseUrl,
      timeout:httpTimeout,
      headers: headers,
      data: JSON.stringify(dataObj)
    }
     return $http(req)
    .then(function(response){
      if (typeID == 1) {
        listMain = response.data[0] ;  <-- Error happening, but being processed in success
        return listMain ;
      } else if (typeID == 2) {
        listPast = response.data[0] ;
        return listPast ;        
      }
    }).catch(function(err) {
      var msg = setError(err) ;
      errMgmt("services/getList",5100,msg) ;
    });
  }

  return {
    getList: function(typeID) {  // typeID : 1=current lsit, 2=past list
      return getList(typeID) ;
    }
 })

我读过很多东西,但我注意到我的服务可能需要定义为:

return $http(req) {
.then(function(success) {   
   // success response
},function(error1) {
   // error response
}).catch(error2) {
   // catch all
}) ;

如果是这样的话,那么两者之间到底有什么区别呢 function(error1).catch(error2) -每一个过程具体做什么?

ojsjcaue

ojsjcaue1#

尝试使用success()和error()调用$http

.factory("listService", function($http,$q) {
  // errMgmt 5100

  var headers={} ;
  var listMain = [] ;  // Current Lists
  var listPast = [] ;  // Past Lists

  function setVars() {
    baseUrl = "https://api.mydomain.com/v1/index.php" ;
    headers = {
      'Pragma':'no-cache', 
      'Expires': -1, 
      'Cache-Control':'no-cache,no-store,must-revalidate', 
      'Content-Type' : 'application/json',        
      'X-Requested-With':'com.mydomain',
      Authorization: 'Token ' +clientToken
    } ;
  }

  function getList(typeID) 
  {
    if (typeID == 1) {
      listMain = [] ;
    } else if (typeID == 2) {
      listPast = [] ;
    }
    setVars() ;
    var dataObj = [{"type":typeID,"userID":userData.user_ID}] ;
    var req = {
      method: 'POST',
      url: baseUrl,
      timeout:httpTimeout,
      headers: headers,
      data: JSON.stringify(dataObj)
    }

    $http(req).success(function(data, status) 
    {
    if (status == 200) 
    {
      alert(data);
      if (typeID == 1) {
        listMain = data ;  
        return listMain ;
      } else if (typeID == 2) {
        listPast = data;
        return listPast ;        
      }       

    }
    }).error(function(data) 
    {
      alert(data);
    });   
  }

  return {
    getList: function(typeID) {  // typeID : 1=current lsit, 2=past list
      return getList(typeID) ;
    }
 })

相关问题