2017-06-22 52 views
0

我正在嘗試修復我爲志願者組織的網站。

我試圖從角1.3.16更新到1.6.4角,但我發現了一個錯誤,指出消息:

TypeError: $http(...).success is not a function at b.$scope.init (angular-custom.js:107)

,這似乎是從導致它的代碼是什麼我可以通過調試它與.success和.error功能角custom.js文件告訴:

$scope.init = function(){ 
     $http({ 
      method: 'post', 
      url: url, 
      data: $.param({ 'type' : 'getUsers' }), 
      headers: {'Content-Type': 'application/x-www-form-urlencoded'} 
     }). 
     success(function(data, status, headers, config) { 
      if(data.success && !angular.isUndefined(data.data)){ 
       $scope.post.users = data.data; 
      }else{ 
       $scope.messageFailure(data.message); 
      } 
     }). 
     error(function(data, status, headers, config) { 
      //$scope.messageFailure(data.message); 
     }); 
    }; 

我也把文件了在plunker The 1.3.16 files at Plunker

我認爲它可能是.success和.error結果,但我不知道如何解決它的Angular。

我是一個自學成才的編碼員,所以任何幫助都會很棒,所以我可以組建並運行這個團隊。

在此先感謝您的任何建議。 杆

+2

是的,'.success','.error'都很久以前就被棄用了,並在1.6版本中完全刪除。一些相關閱讀:http://www.codelord.net/2015/05/25/dont-use-%24https-success/ – CollinD

+1

將其更改爲'.then(function(response){var data = response.data;。 ..' – Phil

+0

感謝您的提示,我想我需要去花時間學習Angular –

回答

1

對於角度1.6.4使用。然後和.catch應對反應和恭敬的錯誤:

(注意,您可以通過使用$ http.post節省一些代碼行)

$scope.init = function(){ 
     $http.post('yourURL', $scope.yourData). 
     then(function(results) { 
      if(results.data.success){ 
       $scope.post.users = results.data; 
      }else{ 
       $scope.messageFailure(results.data.message); 
      } 
     }). 
     catch(function(results) { 
      //$scope.messageFailure(results.data.message); 
     }); 
    }; 
+0

*「next」*?你的意思是*「那麼」*? – Phil

+0

感謝您的接觸 - 更新的答案 – AKMorris

+0

'$ http'允諾用'response'對象解決,而不是'data,status,headers,config' – Phil

3

我建議你閱讀這篇文章 Don't use $http's .success()

而且

   $http({ 
       method: 'post', 
       url: url, 
       data: $.param({'user' : $scope.tempUser, 'type' : 'save_user' }), 
       headers: {'Content-Type': 'application/x-www-form-urlencoded'} 
      }). 
      then(function(response) { 
       your code 
       excuted when post request is success 
      },function(response) { 

       your code 
       excuted when post request is failed 
      }); 

響應從服務器返回,可以調試更深入地探討。

+0

您可以使用.catch的錯誤而不是逗號分隔第三個函數 - 更合理地維護代碼的方式 – AKMorris

+0

@ AKMorris我會說這只是個人喜好 – Phil

+0

謝謝大家,我有點失去了我的深度,必須找到時間打這本老狗的書;-) –

0

感謝大家的幫助,似乎我真的需要花點時間學習Angular,因爲我真的不明白它是如何工作的。

我發現,在我面前的人從AngularJS Insert Update Delete in PHP and MySQL

得到的代碼,但它似乎在少數地方彈出在網絡上爲他人聲稱代碼作爲他們的。可惜。

再次感謝您的輸入,但我認爲我可以在1.5.11版本上運行,而且我將在稍後的日期處理它。

標尺