0

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 :/

6
  • I presume value is an array of strings. Try just sending values through without stringify. Maybe change controller parameter to string[] too Commented Mar 25, 2016 at 4:00
  • @MartinMazzaDawson still not working, and i don't think it has to do with value not being a list since it used to be working before i switched to a web api controller... Commented Mar 25, 2016 at 16:13
  • You are sending a object from javascript, but the controller is expecting an array. have you tried just data: value? Commented Mar 25, 2016 at 16:57
  • Is there a specific reason why you haven't considered a model here - ie Class A { public string[] Urls {get;set;}} then set it up on client side and before sending it do a Json.stringify? Still if you need to just pass the list of strings then you not simply concatenate them using a delimiter and send them in as a giant string and split it on your server side. Commented Mar 25, 2016 at 17:01
  • @peinearydevelopment value is an array, i can't access my code since my PC is closed but value is an array of all the lines in the textarea, also i did try data: value Commented Mar 25, 2016 at 17:09

2 Answers 2

1

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

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

Comments

0

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.

2 Comments

value is an array so when you do a json.stringfy it adds the square bracket.
Yeah..It adds square bracket, but still you get a string value. In console output you will see square brackets because it's a string representation of array. You need to pass actual array object in order to get it properly deserialized into array argument of action method.

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.