簡體   English   中英

角$ http(…)。成功不是函數幫助重寫函數

[英]Angular $http(…).success is not a function help rewriting the function

我正在為一個我自願參加的團體修復一個網站。

我正在嘗試將其從Angular 1.3.16更新到Angular 1.6.4,但是我收到一條錯誤消息,內容為:

TypeError:$ http(...)。成功不是b。$ scope.init的函數(angular-custom.js:107)

通過調試可以看出來的似乎是導致此問題的代碼是帶有.success和.error函數的angular-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的網站上1.3.16文件在Plunker

我知道這可能是.success和.error結果,但是我對Angular的修復方法並不了解。

我是一個自學成才的編碼人員,所以任何幫助都會很大,因此我可以開始這個小組並開始運行。

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

我建議您閱讀本文不要使用$ http的.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
            });

響應是從服務器返回的,您可以進行調試以進行更深入的探索。

對於angular 1.6.4,請使用.then和.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);
        });
    };

感謝大家的幫助,似乎我真的需要抽出一些時間來學習Angular,因為我真的不了解它是如何工作的。

我發現之前的人從AngularJS中獲取了代碼, 並在PHP和MySQL中插入了Delete Delete

但是似乎它在網絡上的幾個地方突然彈出,其他人則聲稱該代碼是他們自己的。 可惜的是。

再次感謝您的輸入,但我想我可以在1.5.11版上使用它,我只需要稍后解決。

竿

暫無
暫無

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

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