0

What is the right approach to get my http request to run again and give new result when I have a new url?

I tried to wrap the whole http call inside another function and call that function from thecontroller. I also tried to run fac.success again. But nothing worked. Thanks for any pointers.

app.factory('fac', ['$http', function(http) { 
    return http.get(url)
    .success(function(result) { 
        return result;})
    .error(function(err) { 
        return err; });
}]);
C8H10N4O2
  • 18,312
  • 8
  • 98
  • 134

2 Answers2

0

You cannot return data in your success and error handlers. The $http call is asynchronous, so the callback functions are called when the promise is resolved...but returning the data to your service after it has run would have no effect.

Instead, consider injecting $http into your controller and calling it from there.

EDIT

Following up on my comment, $http itself is a service, so unless you are doing something more than just making a call, it doesn't make sense to wrap it in another service. Just use the $http service in your controller, like this:

angular.module('myApp')
    .controller('MyController', MyController);

MyController.$inject = ['$http'];
function MyController($http) {
    $http.get('http://path.to.api/')
        .then(function(res) { /* handle success */ })
        .catch(function(err) { /* handle error */ });
}

However, if you want to provide a service that makes a call and caches the response, you could write a service for that. Something like this...

angular.module('myApp')
    .factory('myDataService', myDataServiceFactory);

myDataServiceFactory.$inject = ['$http', '$cacheFactory', '$q'];
function myDataServiceFactory($http, $cacheFactory, $q) {
    var cache = $cacheFactory('myData');

    return {
        getData: getData,
    };

    function getData(url) {
        var data = cache.get(url);
        if (data) {
            var deferred = $q.defer();
            deferred.resolve(data);
            return deferred.promise;
        }
        var promise = $http.get(url);
        promise.then(function(res) {
            cache.put(url, res);
        });
        return promise;
    }
}

...but that example is kind of silly, because $http already offers a configuration for caching response data.

Shaun Scovil
  • 3,905
  • 5
  • 39
  • 58
  • Using a $http again inside the controller worked. Thanks. I was puzzled because the purpose of the service is to separate communication logic. And 'fac.success' returns the right result first. But to do an other http call, it has to be done again. – user1893580 Dec 12 '15 at 18:25
  • No, you only use $http in your controller. Your service as written is useless, because it makes the request but those callbacks do nothing. Using $http in your controller is the correct way to do it, because the controller knows what to do with the response. Only reason to wrap it in a service is if you are caching the response and want to make that data available via your service. – Shaun Scovil Dec 12 '15 at 19:11
0

Deprecation Notice

The $http legacy promise methods .success and .error have been deprecated. Use the standard .then method instead.1

app.factory('fac', ['$http', function(http) { 
    return (
        http.get(url).then( function onFulfilled (result) {
            //return data for chaining 
            return result.data;
        }).catch( function onRejected(response) {
            console.log(result.status);
            //throw to chain rejection  
            throw result;
        })
    );
}]);
georgeawg
  • 48,608
  • 13
  • 72
  • 95