VolleyBall Player
VolleyBall Player

Reputation: 710

Reading Values from GET method for WEB API

I am trying to get a list of items in WebAPI

The ajax methods looks like this

       $.ajax({
            type: 'GET',
            url: 'api/values',
            data: JSON.stringify({ pageNo: pageNo + 1, pageSize: pageSize }), 
            contentType: 'application/json'
       });

In the values controller I am not able to get the values.

my controller looks like this.

    public IEnumerable<string> Get([FromURI] pagingInfo)
    {   
        return new string[] { "value1", "value2" };
    }

What is the correct standard. Is the above standard correct or should put the pageno and pagesize in url and create an another route.

Upvotes: 0

Views: 1256

Answers (1)

cuongle
cuongle

Reputation: 75306

Generally, it is not recommended (or even banned) to send body via GET, just only query string is valid:

Removing method JSON.stringify, your ajax call should be:

$.ajax({
    type: "GET",
    url: "/api/values",
    data: { pageNo: 1, pageSize: 2 },
    success: function (data) {
    }
});

It will automatically convert to query string, if using JSON.stringify, you will get JSON notation in query string and make it wrong. If you take a look on Fiddler, using JSON.stringify, the URL is like:

/api/values?{%22PageNo%22:1,%22PageSize%22:2}

And your Action is till:

public IEnumerable<string> Get([FromURI]PagingInfo pagingInfo)
{   
    return new string[] { "value1", "value2" };
}

Upvotes: 1

Related Questions