0

on my server side (ASP.ne MVC) I have method which looks like this:

    [HttpGet]
    public JsonResult GetTrenings(string treningId)
    {
        var tempId = Guid.Parse(treningId);
        var trening = TreningService.GetTreningById(tempId);
        _trenings = TreningService.GetAllTreningsForUser(trening.UserId);
        return Json(_trenings, JsonRequestBehavior.AllowGet);

    }

And I have Angular service :

publicApp.angularModule.factory('feedSingleTreningService', function ($q, $http) {

return {
   getTrenings: function (data) {
       var input = $http.get("/Feed/GetTrenings", { params: { treningId: data } });


        var deferred = $q.defer();

        deferred.resolve(input);
        return deferred.promise;
    },

};
});

And In my Controller I call this service like this:

 feedSingleTreningService.getTrenings(data).then(function(results) {
        console.log("test", results);
    });

But nothing is shown in console, I've debugged server side and request reaches it, and it returns _trenings, also service returns promise, but nothing happens.

I've changed then to finally, and in console "test" was shown but results were undefined.

Why is this happening ?

1 Answer 1

5

You don't need to defer your call to $http because it already returns a promise.

Just return

return $http.get("/Feed/GetTrenings", { params: { treningId: data } });

then anything that calls your function can do:

getTrenings(myData).then(function(data) { do something }).fail(function() { error });
3
  • I've made changes you've suggested but still then isn't "fired"
    – hyperN
    Mar 25, 2014 at 18:16
  • 2
    did you add a fail() function also to see if you are getting an error? Also use the network tab in firebug/chrome to see if it's making an HTTP request and what the response looks like. Mar 25, 2014 at 18:18
  • It wasn't problem with angular, but with .net, I got error The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.
    – hyperN
    Mar 25, 2014 at 18:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.