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

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

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

  1. .controller('listCtrl', function($scope,listService) {
  2. listService.getList(1)
  3. .then(function(res) {
  4. if (res.list!= null) { // <--- `Exception: Cannot read property 'list' of undefined`
  5. ...
  6. ...
  7. } else {
  8. ...
  9. ...
  10. }
  11. }) ;

服务:

  1. .factory("listService", function($http,$q) {
  2. // errMgmt 5100
  3. var headers={} ;
  4. var listMain = [] ; // Current Lists
  5. var listPast = [] ; // Past Lists
  6. function setVars() {
  7. baseUrl = "https://api.mydomain.com/v1/index.php" ;
  8. headers = {
  9. 'Pragma':'no-cache',
  10. 'Expires': -1,
  11. 'Cache-Control':'no-cache,no-store,must-revalidate',
  12. 'Content-Type' : 'application/json',
  13. 'X-Requested-With':'com.mydomain',
  14. Authorization: 'Token ' +clientToken
  15. } ;
  16. }
  17. function getList(typeID) {
  18. if (typeID == 1) {
  19. listMain = [] ;
  20. } else if (typeID == 2) {
  21. listPast = [] ;
  22. }
  23. setVars() ;
  24. var dataObj = [{"type":typeID,"userID":userData.user_ID}] ;
  25. var req = {
  26. method: 'POST',
  27. url: baseUrl,
  28. timeout:httpTimeout,
  29. headers: headers,
  30. data: JSON.stringify(dataObj)
  31. }
  32. return $http(req)
  33. .then(function(response){
  34. if (typeID == 1) {
  35. listMain = response.data[0] ; <-- Error happening, but being processed in success
  36. return listMain ;
  37. } else if (typeID == 2) {
  38. listPast = response.data[0] ;
  39. return listPast ;
  40. }
  41. }).catch(function(err) {
  42. var msg = setError(err) ;
  43. errMgmt("services/getList",5100,msg) ;
  44. });
  45. }
  46. return {
  47. getList: function(typeID) { // typeID : 1=current lsit, 2=past list
  48. return getList(typeID) ;
  49. }
  50. })

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

  1. return $http(req) {
  2. .then(function(success) {
  3. // success response
  4. },function(error1) {
  5. // error response
  6. }).catch(error2) {
  7. // catch all
  8. }) ;

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

ojsjcaue

ojsjcaue1#

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

  1. .factory("listService", function($http,$q) {
  2. // errMgmt 5100
  3. var headers={} ;
  4. var listMain = [] ; // Current Lists
  5. var listPast = [] ; // Past Lists
  6. function setVars() {
  7. baseUrl = "https://api.mydomain.com/v1/index.php" ;
  8. headers = {
  9. 'Pragma':'no-cache',
  10. 'Expires': -1,
  11. 'Cache-Control':'no-cache,no-store,must-revalidate',
  12. 'Content-Type' : 'application/json',
  13. 'X-Requested-With':'com.mydomain',
  14. Authorization: 'Token ' +clientToken
  15. } ;
  16. }
  17. function getList(typeID)
  18. {
  19. if (typeID == 1) {
  20. listMain = [] ;
  21. } else if (typeID == 2) {
  22. listPast = [] ;
  23. }
  24. setVars() ;
  25. var dataObj = [{"type":typeID,"userID":userData.user_ID}] ;
  26. var req = {
  27. method: 'POST',
  28. url: baseUrl,
  29. timeout:httpTimeout,
  30. headers: headers,
  31. data: JSON.stringify(dataObj)
  32. }
  33. $http(req).success(function(data, status)
  34. {
  35. if (status == 200)
  36. {
  37. alert(data);
  38. if (typeID == 1) {
  39. listMain = data ;
  40. return listMain ;
  41. } else if (typeID == 2) {
  42. listPast = data;
  43. return listPast ;
  44. }
  45. }
  46. }).error(function(data)
  47. {
  48. alert(data);
  49. });
  50. }
  51. return {
  52. getList: function(typeID) { // typeID : 1=current lsit, 2=past list
  53. return getList(typeID) ;
  54. }
  55. })
展开查看全部

相关问题