【问题标题】:Pass parameter to webmethod $http post将参数传递给 webmethod $http post
【发布时间】:2016-10-18 09:46:36
【问题描述】:

我需要将参数传递给下面的 webmethod。我有需要传递给 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;
}); 

请指教。

【问题讨论】:

    标签: angularjs ajax webmethod


    【解决方案1】:

    您可以传递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);
          });
    

    【讨论】:

      【解决方案2】:

      你可以使用$http.post

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

      更多详情请查看official documentation

      【讨论】:

        【解决方案3】:
        $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 发送

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2010-10-18
          • 1970-01-01
          • 2016-05-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-02-04
          • 1970-01-01
          相关资源
          最近更新 更多