简体   繁体   English

即使实例不为null,为什么WCF AJAX方法的参数也不填充值?

[英]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.

相关问题 Ajax sending null values to Controller even though I can see the value isn't null in Ajax - Ajax sending null values to Controller even though I can see the value isn't null in Ajax 即使没有失败,AJAX也会失败吗? - AJAX failing even though it's not failing? 即使是POST方法,Ajax也充当GET方法 - Ajax acting as GET method even though is POST method jquery ajax方法被重复,即使单击事件不正确 - jquery ajax method being repeated even though click event not correct 即使Facebook正在向我提供所请求的数据,Ajax也会一直向该方法发送空值 - Ajax keeps sending null values to the method even tho facebook is giving me the requested data Rails-AJAX请求-即使允许使用数组值也不允许使用参数 - Rails - AJAX request - Unpermitted parameters even though permitted for array values 为什么不应该进行这个ajax调用 - Why is this ajax call being made even though it shouldn't be 为什么我的 ajax get 请求即使成功也响应错误? - Why is my ajax get request responding with an error even though it was successful? s:select以填充ajax响应 - s:select to be populated with ajax response 为什么我的MVC控制器上的数组在Ajax回发时为null - Why would my array on my MVC Controller be null on Ajax Postback
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM