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??

2 Answers 2

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)

0
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

Your Answer

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

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