简体   繁体   中英

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

I'm trying to fix a website for a group I volunteer for.

I'm trying to update it from Angular 1.3.16 to Angular 1.6.4, but I'm getting an error message that says:

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

The code that seems to be causing it from what I can tell by debugging it is the angular-custom.js file with the .success and .error functions:

$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);
        });
    };

I have also put the files up at plunker The 1.3.16 files at Plunker

I understand that it might be the .success and .error results, but I don't know Angular that much in how to fix it.

I'm a bit of a self-taught coder so any help would be great so I can this group up and running.

Thanks in advance for any advice. Rod

I recommend you to read this article Don't use $http's .success()

And

             $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
            });

response is returned from server, you can debug to explore more deeply.

For angular 1.6.4 use .then and .catch to deal with the response and the error respectfully:

(note you can save some lines of code by using $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);
        });
    };

Thanks everyone for your help, it seems that I really need to take some time out and learn Angular as I really don't understand how it works.

I found that the person before me got the code from AngularJS Insert Update Delete in PHP and MySQL

But it seems that it pops up in a few places on the net as others claiming the code as theirs. Pity about that.

Again thanks for the input but I think I can get it working on ver 1.5.11 and I'll just have to work it out at a later date.

Rod

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM