0

I have created a below factory which is supposed to return some data based by taking url and an input paramenter as No.

(function () {
    'use strict'
    angular.module('MainApp').factory('GetData', ['$http', function ($http) {
        return {
            getCountries: function (url, No) {
                return $http({
                    method: "GET",
                    url: url,
                    DNo: No,
                    
                });
            }
        }

    }]);
})();

I have injected this factory to my controller and used as below.

GetData.getCountries('/General/API/GetDetails', $scope.No).then(function (res) {
                   
                    console.log(res.data);
                }, function (res) {

                });

            }

and here is my API.

 public JsonResult GetDetails(string DNo)
        {
            var allCntry = entity.spGetCountries(DNo).ToList();

            return Json(allCntry, JsonRequestBehavior.AllowGet);
        }

All is good, the factory calls the api but does not send the parameter value, my API always set/initialized to null value.

1 Answer 1

1

To apply search parameters to a URL with the $http service, use the params property of the config:

angular.module('MainApp').factory('GetData', ['$http', function ($http) {
    return {
        getCountries: function (url, No) {
            return $http({
                method: "GET",
                url: url,
                //DNo: No,
                //USE params property
                params: { Dno: No }

            });
        }
    }

}]);

config - params{Object.<string|Object>} – Map of strings or objects which will be serialized with the paramSerializer and appended as GET parameters.

-- AngularJS $http Service API Reference - Usage.

Sign up to request clarification or add additional context in comments.

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.