简体   繁体   中英

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. All members of ClientMessage are null in ProcessRequest's data parameter, when the method runs as I debug it.

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. 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.

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).

[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:""}) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM