最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Angularjs $http.get not working - Stack Overflow

programmeradmin8浏览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 ?

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 ?

Share Improve this question asked Mar 25, 2014 at 18:09 hyperNhyperN 2,75410 gold badges58 silver badges96 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 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 });
发布评论

评论列表(0)

  1. 暂无评论