Reputation: 3505
Server Side:
[RestService("/x")]
public class XFilter
{
public long[] CountryIds { get; set; }
}
public class XService : RestServiceBase<XFilter>
{
private const int PageCount = 20;
public override object OnGet(XFilter request)
{
Debugger.Break(); // request is always default at this point !!
return a;
}
}
Client Side:
<script type="text/javascript">
var requestData= "{CountryIds:[1,2]}";
$.getJSON("/api/x", requestData, function (b) {
});
It must be so easy, but I could not get XFilter on server side with this approach.
Upvotes: 1
Views: 486
Reputation: 143319
This is not valid JSON:
var requestData= "{CountryIds:[1,2]}";
All strings must be quoted, e.g:
var requestData= '{"CountryIds":[1,2]}';
var requestData = JSON.stringify({CountryIds:[1,2]}); //same as above
This would be what you would send via $.ajax
POST which you might consider doing since there is no convention to handle complex types in GET requests since it has to serialize it to the queryString.
If you wanted to send a complex type as a queryString you would need to construct the URL yourself. ServiceStack uses its JSV Format to handle complex types in GET requests (which is just JSON with CSV escaping, aka. JSON without quotes).
So your request would now be:
var requestData = "[1,2]";
$.getJSON("/api/x?CountryIds=" + requestData, function (b) {
});
Re-Cap: If you wanted to send JSON you would need to do so via an ajax POST.
Upvotes: 1
Reputation: 3505
var requestData= "{CountryIds:[1,2]}";
Must be
var requestData= {CountryIds:[1,2]};
!
But still there is a problem in getJSON. It does not work with arrays?
Send list/array as parameter with jQuery getJson is solution for it :)
Add this:
$.ajaxSetup({
traditional: true
});
Upvotes: 0