Angularjs $http.get not working

人走茶凉 提交于 2019-12-13 06:59:40

问题


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:


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


来源:https://stackoverflow.com/questions/22642889/angularjs-http-get-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!