使用JQuery将对象传递给WCF服务
我知道这里有一些问题问的完全一样,但尽管我读了它们,我仍然错过了一些东西,希望你能帮助我 使用VS添加的WCF模板非常简单 接口:使用JQuery将对象传递给WCF服务,jquery,wcf,rest,Jquery,Wcf,Rest,我知道这里有一些问题问的完全一样,但尽管我读了它们,我仍然错过了一些东西,希望你能帮助我 使用VS添加的WCF模板非常简单 接口: [OperationContract] [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat= WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)] Comp
[OperationContract]
[WebInvoke(Method = "POST",
ResponseFormat = WebMessageFormat.Json,
RequestFormat= WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Wrapped)]
CompositeType GetDataUsingDataContract(CompositeType composite);
实施:
public CompositeType GetDataUsingDataContract(CompositeType composite){
if (composite == null)
{
throw new ArgumentNullException("composite");
}
if (composite.BoolValue)
{
composite.StringValue += "Suffix";
}
return composite;
}
Javascript:
$.ajax({
type: "POST",
url: "http://localhost:1545/Service1.svc/GetDataUsingDataContract",
data: JSON.stringify(compType),
contentType: "application/json; charset=utf-8",
dataType: "json",
processdata: true,
success: function (msg) {
$("#txtTest").val(message.BoolValue + " : " + message.StringValue);
},
error: function (xhr, errorMsg, thrown) {
$("#error").html(xhr.responseText);
}
}))
我得到的错误是:
服务器在处理请求时遇到错误。异常消息为“值不能为null”。参数名称:composite
因此,该值没有被传递,当它到达WCF服务时为null
*注意:我尝试过调用使用字符串并返回复合类型的方法,效果很好*
Thx提前为您提供帮助以下是如何构造compType,以便
JSON.stringify()
将产生服务所期望的结果:
var compType = { composite: { StringValue: "MyString", BoolValue: true } };
下面是如何构造compType,以便
JSON.stringify()
将产生服务所期望的结果:
var compType = { composite: { StringValue: "MyString", BoolValue: true } };
我也有同样的问题。。你得到解决方案了吗?如果你取消If(composite.BoolValue),你有相同的问题吗?我有相同的问题。。您得到解决方案了吗?如果您取消If(composite.BoolValue),您是否有相同的问题