1

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.

2 Answers 2

1

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);
         }); 
    };
}
Sign up to request clarification or add additional context in comments.

Comments

0

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);
         }); 
    };
});

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.