0

When calling a REST service with jquery, I can pass a POJO object to $.get(...) call, and jquery will serialize the object's properties to GET QueryString.

My REST service is developed using WebAPI, and I'm wondering if I can make it automatically deserialize querystring parameters to an object.

More precisely:

If I have a POST method, I can deserialize the json from message body as below

I have the following jQuery code

var dataObj // my POJO
j$.post('/api/myserv', dataObj, function (data, status, jqXHR) {
    // ....

And the WebApi endpoint

' MyEntity class has same structure as POJO object
Public Function PostValue(<FromBody()> ByVal entity As MyEntity) As HttpResponseMessage 
    ' here I have entity instantiated and setup

When using jquery get, and passing POJO object:

var dataObj // my POJO
j$.get('/api/myserv', dataObj, function (data, status, jqXHR) {
    // ....

This translates to

GET /api/myserv?prop1=val1&prop2=val2&...

In WebApi I would like to instantiate an object as with POST

' MyEntity class hadata from uri, s same structure as POJO object
Public Function GetValue(<FromURI()> ByVal entity As MyEntity) As HttpResponseMessage 
    ' but here the entity is null

To be able to read I have to have a param for every prop in POJO object, like

' MyEntity class hadata from uri, s same structure as POJO object
Public Function GetValue(prop1 As .., prop2 as ..., ) As HttpResponseMessage 

Is there any way to be able to read all params from get in a single object, as it'spossib;e with POST from body?

Thank you

1 Answer 1

3

You can use [FromUri] before the object in the WebAPI method parameter. Take a look at Parameter Binding here.

Set your jquery call up like below:

$.ajax({
    url: url,
    type: 'GET',
    dataType: 'json',
    data: { prop1: 1, prop2: "two" },
    success: function (result) {
        //handle
    }
});
Sign up to request clarification or add additional context in comments.

1 Comment

Actually I already did it (see my question), and it didn't worked. But now I tried again, and it did worked now. Strange. Thanks for answer, that made me try again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.