1

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.

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

No. I've added response. PLease check it. IT must be used without quotations in javascript.
0
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
});

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.