Surendra Mourya Surendra Mourya - 1 month ago 7
Ajax Question

Pass parameter to webmethod $http post

I need to pass parameter to the webmethod below. I have value Param which need to be passed to webmethod.

var param='test'
$http({
method:"POST",
url:'/sites/Demo/_layouts/15/demo/Demo.aspx/mywebmethod',
data: JSON,
headers: {
'Content-Type': 'application/json'
}
})
.then(function(data)
{
$scope.Jobject = data.data.d;
});


Please advise.

Answer

You may pass params key for parameters or data key form body data which is not available in GET method

var params = {
  name: 'This is parameter query'
}
var data = {
  name: 'This is body data'
}
$http({
  method: 'POST',
  url: '/sites/Demo/_layouts/15/demo/Demo.aspx/mywebmethod',
  data: data,
  params: params
}).then(function successCallback(response) {
    $scope.Jobject = response.data.d;
  }, function errorCallback(response) {
    console.log(response);
  });

or simply use

$http.post('/sites/Demo/_layouts/15/demo/Demo.aspx/mywebmethod', data).then(function successCallback(response) {
        $scope.Jobject = response.data.d;
      }, function errorCallback(response) {
        console.log(response);
      });