Why would a WCF AJAX method's argument not be populated with values, even though the instance is not null?

204 views Asked by At

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?
1

There are 1 answers

1
Triynko On

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