$http POST response from service to controller
Asked Answered
A

2

6

How to get the response from Service in below case??

Service:

app.factory('ajaxService', function($http) {
    updateTodoDetail: function(postDetail){
        $http({
            method: "POST",
            headers: {'Content-Type': 'application/x-www-form-urlencoded'},
            url: post_url,
            data: $.param({detail: postDetail})
        })
        .success(function(response){
            //return response;
        });
    }
})

Controller:

updated_details = 'xyz';
ajaxService.updateTodoDetail(updated_details);

In th above case, i POST the data through Controller and it was working fine but now i want the response to come in my Controller.

How to achive that??

Amphitropous answered 23/6, 2014 at 3:38 Comment(0)
C
10

$http returns a promise:

Return the promise

updateTodoDetail: function(postDetail){
    return $http({
        method: "POST",
        headers: {'Content-Type': 'application/x-www-form-urlencoded'},
        url: post_url,
        data: $.param({detail: postDetail})
    });

So you can do

ajaxService.updateTodoDetail(updated_details).success(function(result) {
    $scope.result = result //or whatever else.
}

Alternatively you can pass the successfunction into updateTodoDetail:

updateTodoDetail: function(postDetail, callback){
    $http({
        method: "POST",
        headers: {'Content-Type': 'application/x-www-form-urlencoded'},
        url: post_url,
        data: $.param({detail: postDetail})
    })
    .success(callback);

So your controller has

ajaxService.updateTodoDetail(updated_details, function(result) {
    $scope.result = result //or whatever else.
})

I would prefer the first option so I could handle errors etc as well without passing in those functions too.

(NB: I haven't tested the code above so it might require some modification)

Capacitance answered 23/6, 2014 at 3:48 Comment(0)
B
0

what I usually do is like this

app.factory('call', ['$http', function($http) {
//this is the key, as you can see I put the 'callBackFunc' as parameter
function postOrder(dataArray,callBackFunc) {
                    $http({
                        method: 'POST',
                        url: 'example.com',
                        data: dataArray
                    }).
                            success(function(data) {
                                 //this is the key
                                 callBackFunc(data);
                            }).
                            error(function(data, response) {
                                console.log(response + " " + data);
                            });
                }

    return {
            postOrder:postOrder
           }
}]);

then in my controller I just call this

    $scope.postOrder = function() {
    call.getOrder($scope.data, function(data) {
      console.log(data);
      }
   }

do not forget to insert the dependency injection of 'call' services into your controller

Barringer answered 23/6, 2014 at 4:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.