-1

service.js

angular.module('users.background.image', [])
    .factory('changebgimage', ['$http', '$q', '$rootScope', function($http, $q, $rootScope){

     'use strict';  

     var service = {
        changeRandomImage : changeRandomImage
     };

     function changeRandomImage(){

        /*var random = $scope.data[Math.floor(Math.random() * $scope.data.length)];
        $scope.imageSource = random;*/

        var url = 'webService/imageUrl.htm';
        
        var deferred = $q.defer();

        $http.get(url)
            .success(function(data){
                deferred.resolve(data.data);
            })
            .error(function(error){
                deferred.reject(error.message);
            });

            return deferred.promise;
    }

    return service;

   }]);

aa.js

angular.module('users.da', ['users.background.image'])
  .controller('da', ['user', '$scope', 'changebgimage', function (user, $scope, changebgimage) {
    'use strict';

     changebgimage.changeRandomImage()
        .success(function (data) {
          console.log(data);
        }).error(function(error){
          console.log(error);
        });
  }]);

imageUrl.htm

    [{
            "id": 1,
            "path": "images/bg/1.jpg"
          },
          {
            "id": 2,
            "path": "images/bg/2.jpg"
          },
          {
            "id": 3,
            "path": "images/bg/3.jpg"
          },
          {
            "id": 4,
            "path": "images/bg/4.jpg"
          },
          {
            "id": 5,
            "path": "images/bg/5.jpg"
          },
          {
            "id": 6,
            "path": "images/bg/6.jpg"
     }]

I am trying to get the value from service in angular but getting error changebgimage.changeRandomImage(...).success is not a function I can't understand why i am getting this error. Can anyone please take a look and assist me what is doing wrong on it promise is passing correctly.

Community
  • 1
  • 1
Akki Verma
  • 49
  • 7
  • Possible duplicate of [Error with $http.get in angularJS -- Success not a Function](http://stackoverflow.com/questions/41183845/error-with-http-get-in-angularjs-success-not-a-function) – georgeawg May 05 '17 at 12:29
  • The `.success` and `.error` methods are [deprecated and removed from AngularJS V1.6](http://stackoverflow.com/questions/35329384/why-are-angular-http-success-error-methods-deprecated-removed-from-v1-6/35331339#35331339). No new code should use those methods. – georgeawg May 05 '17 at 17:48
  • There is no need to manufacture a promise with `$q.defer` as the $http service already returns a promise. Some people consider it to be an anti-patten. See [Is this a “Deferred Antipattern”?](http://stackoverflow.com/questions/30750207/is-this-a-deferred-antipattern). – georgeawg May 05 '17 at 17:54

5 Answers5

1

The $http service is a function which takes a single argument — a configuration object — that is used to generate an HTTP request and returns a promise. Follow $http Service Documentation

// Simple GET request example:
$http({
  method: 'GET',
  url: '/someUrl'
}).then(function successCallback(response) {
    // this callback will be called asynchronously
    // when the response is available
  }, function errorCallback(response) {
    // called asynchronously if an error occurs
    // or server returns response with an error status.
  });
Govinda Rajbhar
  • 2,926
  • 6
  • 37
  • 62
  • Im getting correct response from service but while im using this service to other controller it is showing error changebgimage.changeRandomImage(...).success – Akki Verma May 05 '17 at 11:59
  • `$http.get()` is a perfectly valid and documented shortcut. The issue here is that the `success()` and `error()` methods are deprecated on the `$http` service (i.e. they still work) and removed on the `$q` service which is the cause of the error. – Lennholm May 05 '17 at 13:23
1

A promise object doesn't have success() or error() methods, it has then() and catch() (and also finally()) methods

Lennholm
  • 7,205
  • 1
  • 21
  • 30
0

.success() method is deprecated, use .then().

Like:

$http.get(url).then(function (data) {
 // handle response data
}, function (error) {
  // handle error from api
}).catch(function (err) {
  // handle error from response.
});
anoop
  • 3,812
  • 2
  • 16
  • 28
0

Looks like they removed that function in new version. Try to use promises with "then"

$http.get(url).then(function(response) {
    // data is under response.data
});

There is an article on this: http://www.codelord.net/2015/05/25/dont-use-%24https-success/

-1

Removed deferred.resolve(data.data); because data.data has no value data has the value

function changeRandomImage(){

    var url = 'webService/imageUrl.htm';

    var deferred = $q.defer();

    $http.get(url)
        .success(function(data){
            deferred.resolve(data);
        })
        .error(function(error){
            deferred.reject(error.message);
        });

        return deferred.promise;
}
Akki Verma
  • 49
  • 7
  • There is no need to manufacture a promise with `$q.defer` as the $http service already returns a promise. Some people consider it to be an anti-patten. See [Is this a “Deferred Antipattern”?](http://stackoverflow.com/questions/30750207/is-this-a-deferred-antipattern). – georgeawg May 05 '17 at 17:54
  • Yes it is. 'Deferred anti-pattern' happens when new redundant deferred object is created to be resolved from inside an promise chain. – Akki Verma May 08 '17 at 12:08