[英]Why would a WCF AJAX method's argument not be populated with values, even though the instance is not null?
I sent an AJAX request to my server using jQuery.ajax, and the "data" field is populated with a JSON string corresponding to my ClientMessage type, but the data isn't making it to the method's main parameter. 我使用jQuery.ajax向服务器发送了AJAX请求,“数据”字段中填充了与我的ClientMessage类型相对应的JSON字符串,但数据并未将其传递给方法的主要参数。 All members of ClientMessage are null in ProcessRequest's data parameter, when the method runs as I debug it.
当该方法在我调试时运行时,ClientMessage的所有成员在ProcessRequest的data参数中均为null。
The ClientMessage class's members are simple auto-implemented get/set string properties, decorated with the DataMember(Name="membername") attribute along with the DataContract attribute on the class itself. ClientMessage类的成员是简单的自动实现的获取/设置字符串属性,使用类本身的DataMember(Name =“ membername”)属性以及DataContract属性进行修饰。 On the client side, Chrome's developer tool shows that the AJAX request's data object is valid JSON and I can unfold it in the request viewer to verify the correct object and members are being sent.
在客户端,Chrome的开发人员工具显示AJAX请求的数据对象是有效的JSON,我可以在请求查看器中将其展开以验证是否发送了正确的对象和成员。
I just cannot figure out why the ClientMessage data instance is arriving with null values for all its members (they are all simple strings, by the way). 我只是无法弄清楚为什么ClientMessage数据实例的所有成员都以null值到达(顺便说一下,它们都是简单的字符串)。
[OperationContract]
[WebInvoke(Method="POST",BodyStyle=WebMessageBodyStyle.Wrapped,RequestFormat=WebMessageFormat.Json,ResponseFormat=WebMessageFormat.Json)]
public ServerResponse ProcessRequest( ClientMessage data )
{
//all members of "data" parameter are null, why?
Nevermind. 没关系。 Apparently you have to send the data object in a wrapper object with member names matching the parameter names of the method.
显然,您必须在成员名称与方法的参数名称匹配的包装对象中发送数据对象。 So instead of making the AJAX call using settings like
data:JSON.stringify({username:"",password:""})
I'd have to use data:JSON.stringify({data:{username:"",password:""})
. 因此,
data:JSON.stringify({username:"",password:""})
使用诸如data:JSON.stringify({username:"",password:""})
类的设置进行AJAX调用data:JSON.stringify({username:"",password:""})
我不得不使用data:JSON.stringify({data:{username:"",password:""})
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.