最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin3PV0评论

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 });
Post a comment

comment list (0)

  1. No comments so far