How to get data returned from a post request

1

I have the following code:

$http.post('data.php').success(function(data) {
    return data;
}).error(function(data) {
    console.log(data);
}); 

How to manipulate the data coming from this request?

I'm doing it this way:

var data = $scope.get_notas();

But when I use the variable date it returns me undefined

    
asked by anonymous 28.05.2015 / 03:50

1 answer

1

The problem is that the http call is asynchronous. One way to solve it would be to rewrite its get_notas function so that it executes a callback function:

$scope.get_notas = function(callback) {
    $http.post('data.php')
        .success(callback)
        .error(function(data) {
           console.log(data);
        }); 
}

And when calling it, pass the function that must be executed when completing the request:

$scope.get_notas(function(data) {
    console.log(data);
});
    
28.05.2015 / 16:15