28

I know there are tons of answers for this topic, but couldn't find the solution to my issue. I have an ASP.NET MVC Web API that looks like this:

    [HttpGet]
    public IList<Country> GetCountryList(List<long> idList)

And I've tried calling it like this:

    $.ajax({
        dataType: "json",
        data: JSON.stringify({idList: listOfIds}),            
        type: "GET",
        url: "api/v1/util/CountryList",
        success: function (result) {
            alert(result);
        }
    });

The URL then looks like this:

https://localhost/supertext/api/v1/util/CountryList?{%22idList%22:[46,14,62,83,120]}

Alternative:

    $.ajax({
        dataType: "json",
        data: {
            idList: JSON.stringify(listOfIds),
        }          
        type: "GET",
        url: "api/v1/util/CountryList",
        success: function (result) {
            alert(result);
        }
    });

URL:

https://localhost/supertext/api/v1/util/CountryList?idList=%5B46%2C14%2C62%2C83%2C120%5D

Both methods don't work.

Do I really have to send and receive it as a string or use POST?

2 Answers 2

53

No, don't try to be sending JSON in a GET request. Use JSON with other verbs which have body, such as POST and PUT.

Do it the standard way, by decorating your action parameter with the [FromUri] attribute:

public IList<Country> GetCountryList([FromUri] List<long> idList)
{
    ...
}

and then just trigger the AJAX request:

$.ajax({
    url: 'api/v1/util/CountryList',
    type: 'GET',
    data: { idList: [1, 2, 3] },
    traditional: true,
    success: function (result) {
        console.log(JSON.stringify(result));
    }
});

Further recommended reading for you about how the model binding in the Web API works:

http://www.asp.net/web-api/overview/working-with-http/sending-html-form-data,-part-1

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

3 Comments

Thanks! This works if I hardcode data: { idList: [1, 2, 3] }, but not if I use data: JSON.stringify({idList: listOfIds}),
But why are you stringifying???? Don't JSON.stringify anything! I already told you not to use JSON with a GET request in my answer. Your request should look like this: data: { idList: listOfIds }. Obviously I assume that listOfIds is a javascript array of numbers.
Sorry, took a little longer to understand all of it. Working now! Thanks a lot.
0

**Following are two parameter Enum and objSearch **

var Enum = "ABCD";

var objSearch = [
              {"Name":"Navjot Angra","Age":23},
              {"Name":"Nav","Age":22}];

//this is ajax method

$.ajax({

   type: "GET",
   var GatwayUrl ='http//2937/' (//Your url)
   url: GatwayUrl + 'api/Certificate/GetDetail/?Enum=' + Enum +'+&objSearch='+ JSON.stringify(objSearch),
   contentType: "application/json; charset=utf-8",
   dataType: "json",
   success: function (result) {
     if (result) {
        alert("Your Code");    
        }       }
});

//this part is web api part

[HttpGet]

public IHttpActionResult Fetch([FromUri]string Enum, [FromUri]string objSearch) {

IHttpActionResult action=null; return action;

}

Comments

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.