我正在使用wcf服务,我将按以下方法发送一个列表。 但我想发送两个参数,如下面的方法,可以是一个列表或任何对象与服务层中的实体相同的post方法。 这可能吗?
以下是我向Service发送对象数组的过程。它运行良好。
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");
}
});
WCF:
[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; }
}
由于
答案 0 :(得分: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");
}
});
答案 1 :(得分: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+'}';
参数名称必须在服务器和客户端都匹配。