将参数传递给 webmethod $http post

Pass parameter to webmethod $http post

我需要将参数传递给下面的网络方法。我有需要传递给 webmethod 的值 Param。

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

请指教

您可以使用$http.post

$http.post('/sites/Demo/_layouts/15/demo/Demo.aspx/mywebmethod', params)

有关详细信息,请查看 official documentation

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

假设您想将 params 字符串作为 name

发送

您可以传递 params 参数键或 data 键表单正文数据,这在 GET 方法

中不可用
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);
  });

或者直接使用

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