0

I'm using mvc5 web API. I'm trying pass multiple parameters to web API web method. I need filter result using these parameters. My code: JavaScript:

var options = {
 url: '/api/normdata/getdata',
 type: 'GET',
 dataType: 'json',
 data: [{ 'name': 'n1' }, { 'name': 'n2' }, { 'name': 'n3' }, { 'name': 'n4' }]
};
$.ajax(options).then(querySucceeded).fail(queryFailed);

function querySucceeded(data) {
   var objectArray = [];
       ...
  }

function queryFailed(jqXHR, textStatus) {
   var msg = 'Error retreiving data. ' + jqXHR + " " + textStatus;
   errorMessage(msg);
}

Code in web API controller:

  [Route("api/normdata/getdata")]
    public IEnumerable<string> getdata([FromBody] List<object> somedata)
    {
        List<string> stringList = new List<string>();
        var o = somedata;
        return stringList;
    }

The problem is that somedata variable is always null. Please advise.

0

2 Answers 2

1

You're sending a GET request, but specifying [FromBody] in the controller. Try changing your request to a POST.

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

3 Comments

Hi, thanks for answer. Unfortunately after i have changet to 'POST' getdata method is not called at all. thanks. leszek
Any errors in the javascript console or network tab of your browser's dev tools? Any exceptions on the server? Do you have the [HttpGet] attribute on your action?
I have added [HttpPost] attribute and then the function is called. Unfortunately number of objects (somedata) = 0. Thanks.
0

Try this,

var data = [{ 'name': 'n1' }, { 'name': 'n2' }, { 'name': 'n3' }, { 'name': 'n4' }]
var options = {
    url: '/api/normdata/getdata',
    type: 'POST',
    dataType: 'json',
    data: JSON.stringify(data)

};
$.ajax(options).then(querySucceeded).fail(queryFailed);

function querySucceeded(data) {
    var objectArray = [];
   ...
}
function queryFailed(jqXHR, textStatus) {
    var msg = 'Error retreiving data. ' + jqXHR + " " + textStatus;
    errorMessage(msg);
}

Documentation link

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.