I have an AJAX call that I want to run against a WCF GET service. Basically, the call to the service (via jquery) looks like this:
$.get(serviceEndpoint, {query : "some search text", statusTypes: [1, 2]}, function (result) { /* do something*/ }, 'text');
When this call gets run, I see the GET in firebug go through correctly, and I do hit the endpoint. However, the parameter statusTypes
is always null.
The GET itself from jquery looks like it is encoded, but when I don't encode the brackets, the call won't enter the endpoint at all:
And the WCF service itself:
[OperationContract]
[WebInvoke(Method= "GET", BodyStyle = WebMessageBodyStyle.WrappedRequest,
ResponseFormat =
WebMessageFormat.Json)]public
ResultsViewModel
GetTags(string query, int[]
statusTypes)
Is it possible to pass an array via GET to a WCF service?
The permutations aren't numerous, so I could write an individual endpoint "per array", but I'd rather keep it in one.
Best Answer
It is possible, but not with the out-of-the-box WCF. With the "jQuery support" in the WCF codeplex page, you can receive all of the data sent by jQuery (including arrays, nested objects, etc) in an untyped variable, both on the body (for POST requests) and on the query string (for GET). The mapping between jQuery array variables (whose names contain '[' and ']') and operation parameters cannot be done in WCF 4.0 (at least not without writing a message formatter).
This should be simpler, however, on the new WCF Web APIs (also available on the codeplex site).
Update: this is an example of a formatter which works for your scenario: