0

In AngularJS v1.6.9 I use HTTP post method for calling my API but $$state give me null so I am not able to give my list. I try to debug result = response.data give me to list but when that API Call service but post API function return also the list but in a controller, I enable to fetch that data.Please help me with that

Module is

 var adminDashbord = angular
.module('mymodel', ['ui.router', 'ui.bootstrap', 'datatables', 'datatables.buttons']);

My API Call Service is

mymodel.service('ApiCall', ['$http', function ($http) {

var result;
var retResult;
var data;


this.PostApiCall = function (controller, method, jData) {
    jData = JSON.stringify(jData);
    result = $http.post('http://localhost:2153/' + controller + '/' + method, jData).then(function onSuccess(response)
    {

        result = response.data;

    })
        .catch(function onError(response)
    {
        console.error("CustomError:" + response.data);
        console.error("Status:" + response.status);
    });


    return result;
};

Post API in a controller like

mymodel.controller('productmasterController', ['$scope', 'ApiCall', 
   function ($scope, ApiCall) {
        $scope.init = function () 
         {
           $scope.getAllproduct();
         }

      $scope.getAllproduct = function () {
       var reqdata = null;
       result = ApiCall.PostApiCall("Product", "SelectAllProduct", reqdata).then(function (response) {


           $scope.getProduct = response.ProductList;



       });
   };

 }]);

My issue was $$state give an empty result.

Tushar
  • 182
  • 3
  • 19

1 Answers1

1

You have two options:

result = $http.post(url).then(function(response){
    return response.data;
})
return result;

OR

return $http.post(url).then(function(response){
    return response.data;
})

Either way you need to return an Http Promise, which $http.get request returns. If you want your promise contain any data, you also need to return the response inside it, hence the second return is used (for response.data).

Aleksey Solovey
  • 4,153
  • 3
  • 15
  • 34