iamanoob
iamanoob

Reputation: 208

ASP.NET Passing json POST data to Web API Results in null data

so i'm trying to send data from a textarea to my ApiController and it doesn't seem to work at all. this is my ajax ( where value is a string array ):

function doCallback(value)
{
$.ajax({
     url: 'http://localhost:83048/api/test/function',
     type: 'POST'
     contentType: "application/json"
     dataType: 'json'
     data:
     { urls : JSON.stringify(value) },
     success......});
 }

Controller :

[HttpPost]
public async Task<List<string>> function(List<string> urls)
{
    return await Program.blabla(urls);
}

The urls are always null, even though the value passed in the ajax is not ( verified using the debugger )

A little help would be appreciated because i can't seem to figure it out after passing more then an hour on this.

EDIT : I've tried googling more and more and it seems like it is stil returning null ( when stringified ) and nothing when not. Also, this has been happenning since i've changed the controller into an ApiController, it was working before changing to the Web Api Controller if it can helps.

EDIT 2 : Tried everything in the comments, still nothing working :/

Upvotes: 0

Views: 838

Answers (2)

iamanoob
iamanoob

Reputation: 208

Changing the data to data: JSON.stringify(value) made it work

$.ajax({
 url: 'http://localhost:83048/api/test/function',
 type: 'POST'
 contentType: "application/json"
 dataType: 'json'
 data: JSON.stringify(value),
 success......});
 }

All that trouble for .. dam it ahah

Upvotes: 1

Mahesh Chavda
Mahesh Chavda

Reputation: 593

Try to specify your urls property as array. Meaning instead of passing

{ urls : JSON.stringify(value) }

Pass like

{ urls : [JSON.stringify(value)] }

Notice the square brackets.

Upvotes: 0

Related Questions