3

我有一个 wcf 服务,我将按照以下方法发送一个列表。但是我想通过 post 方法发送两个参数,如下面的方法意味着可以是列表或任何与服务层中的实体相同的对象。这可能吗?

下面是我将对象数组发送到服务的过程。它运行良好。

jQuery代码:

      var listOfObjects=new Array();
      //creating list of objects
      for(var i=0;i<5;i++)
       {   var MyEntity=new Object();
           MyEntity.TestId =i;
           MyEntity.TestId =i+"testName";
           listOfObjects.push(MyEntity);
       }

       var jsonList=JSON.stringify(listOfObjects);
       var dataToSend = '{"myEntity":'+jsonData+'';

        //Saving info
        $.ajax({
            type: "POST",
            async: false,
            data: dataToSend,
            url: "../ServiceLayer/myService.svc/SaveResults",
            contentType: "application/json; charset=utf-8",
            dataType: "json",          
            success: function () {
                alert("success");
            },
            error: function () {
                alert("Error");
            }
        });

周转基金:

    [WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json,
     ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
    public void SaveLabResults(List<MyEntity> myEntity)
    {
          var lstEntities=myEntity;
    }

实体:

[DataContract]
public class MyEntity
{
    [DataMember]
    public string TestId { get; set; }
    [DataMember]
    public string TestName { get; set; }
}

谢谢

4

2 回答 2

1

当然可以,只需添加另一个参数。

   [WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json,
     ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
    public void SaveLabResults(List<MyEntity> myEntity,List<MyEntity> myEntity2)
    {
          var lstEntities=myEntity;
          var lstEntities2=myEntity2;
    }

和:

var dataToSend = '{"myEntity":'+jsonData+', "myEntity2":'+jsonData2+' }';
//Saving info
$.ajax({
    type: "POST",
    async: false,
    data: dataToSend,
    url: "../ServiceLayer/myService.svc/SaveResults",
    contentType: "application/json; charset=utf-8",
    dataType: "json",          
    success: function () {
        alert("success");
    },
    error: function () {
        alert("Error");
    }
});
于 2013-03-25T16:20:43.030 回答
0

是的,可以发送多个参数,您只需在 Web 方法和客户端都声明它们:

[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json,
 ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
public void SaveLabResults(List<MyEntity> myEntity, SomeObject secondParameter)
{
      var lstEntities=myEntity;
}

在客户端:

var dataToSend = '{"myEntity":'+jsonData+', "secondParameter":'+secondObject+'}';

参数名称必须在服务器端和客户端都匹配。

于 2013-03-25T16:21:54.843 回答