user8060797
user8060797

Reputation: 11

Angular - Factory http post returning undefined value

i searched and researched but i cannot solve my problem.

I'm not so practice with angular, i hope someone can help me!

I created a service for http POST and i must get the result from my controller

CONTROLLER

tantoSvagoApp.controller("ricercaAttivita", function ($scope, 
serviceRegioni, serviceRicercaAttivita) {

$scope.regioni = serviceRegioni.get();
var parameters = {
                "MasterID": 14,
                "NumPart": "",
                "Text": "",
                "Location": {"Region": "", "Province": ""},
                "Attributes": [],
                "Price": {"MinPrice": "","MaxPrice": ""},
                "ProjectCode": "WS678",
                "Pager": {"PageSize": 10,"PageOffset": 1}
            };

$scope.elencoAttivita = serviceRicercaAttivita.getAttivita(parameters);
});

SERVICE

tantoSvagoApp.service('serviceRicercaAttivita', function ($http) {
this.getAttivita = function(arr) { 
        $http({
              method: 'POST',
              url: 'http://localhost/Tantosvago/api/getAttivita.php',
              data: arr
         }).then(function successCallback(response) {
                var result = response.data;
                return result;
         }, function (response) {
                console.log("Errore " + response.data,response.status);
         }); 
    };
}
);

What i get in UNDEFINED.

Upvotes: 1

Views: 766

Answers (2)

Davi Alves
Davi Alves

Reputation: 51

The $http run asynchronous, so when you call your code just make the call and continue; You can check it putting a return statement in your getAttivita function, like this:

this.getAttivita = function(arr) {
    $http({
              method: 'POST',
              url: 'http://localhost/Tantosvago/api/getAttivita.php',
              data: arr
         }).then(function successCallback(response) {
                var result = response.data;
                return result;
         }, function (response) {
                console.log("Errore " + response.data,response.status);
         }); 
    };

    return "Test";
}

To do what you want you'll need pass a callback function to tour function, and in this callback function assign the value to your $scope porperty:

In your controller:

function Callback(elencoAttivita) { 
    $scope.elencoAttivita = elencoAttivita;
}

serviceRicercaAttivita.getAttivita(parameters, Callback);

and in your service:

this.getAttivita = function(arr, Callback) { 
        $http({
              method: 'POST',
              url: 'http://localhost/Tantosvago/api/getAttivita.php',
              data: arr
         }).then(function successCallback(response) {
                var result = response.data;
                Callback(result);
         }, function (response) {
                console.log("Errore " + response.data,response.status);
         }); 
    };
}

Upvotes: 1

Suneet Bansal
Suneet Bansal

Reputation: 2702

Success and Failure Callbacks of $http.post() are the asynchronous methods where you can not return any value.

Solution is given below

CONTROLLER

tantoSvagoApp.controller("ricercaAttivita", function ($scope, 
serviceRegioni, serviceRicercaAttivita) {

$scope.regioni = serviceRegioni.get();
var parameters = {
                "MasterID": 14,
                "NumPart": "",
                "Text": "",
                "Location": {"Region": "", "Province": ""},
                "Attributes": [],
                "Price": {"MinPrice": "","MaxPrice": ""},
                "ProjectCode": "WS678",
                "Pager": {"PageSize": 10,"PageOffset": 1}
            };
function successCallBack(result) {
    $scope.elencoAttivita = result;
}

serviceRicercaAttivita.getAttivita(parameters, successCallBack);

});

SERVICE

tantoSvagoApp.service('serviceRicercaAttivita', function ($http) {
this.getAttivita = function(arr, successCallBack) { 
        $http({
              method: 'POST',
              url: 'http://localhost/Tantosvago/api/getAttivita.php',
              data: arr
         }).then(function successCallback(response) {
                successCallBack(response.data);
         }, function (response) {
                console.log("Errore " + response.data,response.status);
         }); 
    };
});

Upvotes: 0

Related Questions