Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Post array of strings to web API method

this is my client side ajax call:

    var list = ["a", "b", "c", "d"];

    var jsonText = { data: list };

    $.ajax({
        type: "POST",
        url: "/api/scheduledItemPriceStatus/updateStatusToDelete",
        data: jsonText,
        dataType: "json",
        traditional: true,
        success: function() { alert("it worked!"); },
        failure: function() { alert("not working..."); }
    });

this is chrome network header:

Request URL:http://localhost:2538/api/scheduledItemPriceStatus/updateStatusToDelete

Request Method:POST

Request Headersview source

Accept:application/json, text/javascript, */*; q=0.01

Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3

Accept-Encoding:gzip,deflate,sdch

Accept-Language:en-US,en;q=0.8

Connection:keep-alive

Content-Length:27

Content-Type:application/x-www-form-urlencoded; charset=UTF-8

Host:localhost:2538

Origin:http://localhost:2538

Referer:http://localhost:2538/Pricing/ScheduledItemPrices

User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.97 Safari/537.11

X-Requested-With:XMLHttpRequest

Form Dataview URL encoded

data:a
data:b
data:c
data:d

this is my webapi controller method:

public HttpResponseMessage UpdateStatusToDelete(string[] data)

result:

when I debug, the data parameter in UpdateStatusToDelete returns {string[0]} instead of data:a data:b data:c data:d

What am I doing wrong? Any help is really appreciated.

like image 489
Eunyoung Ro Avatar asked Jan 08 '13 05:01

Eunyoung Ro


People also ask

How do I pass an array to API?

First step is to build the list and assign to a global (let's use $list). Probably would read the source (could be a file, db query, xml/json from an inbound api call), build a loop using While() and append values to $list (like $list = $list + ",'"+sourcelist[count]+"'").

How do I post data on Web API?

Here, we will implement POST method in the Web API. The HTTP POST request is used to create a new record in the data source in the RESTful architecture. So let's create an action method in our StudentController to insert new student record in the database using Entity Framework.

How do you post values in API?

To send data to the REST API server, you must make an HTTP POST request and include the POST data in the request's body. You also need to provide the Content-Type: application/json and Content-Length request headers. Below is an example of a REST API POST request to a ReqBin REST API endpoint.


2 Answers

You should pass the list itself, and not any other object wrapping it.

E.g. pass the following:

var list = ["a", "b", "c", "d"];

in

$.ajax({
        type: "POST",
        url: "/api/scheduledItemPriceStatus/updateStatusToDelete",
        // Pass the list itself
        data: list, 
        dataType: "json",
        traditional: true,
        success: function() { alert("it worked!"); },
        failure: function() { alert("not working..."); }
    });

Your method signature on server is correct.

like image 132
Shaulian Avatar answered Nov 13 '22 11:11

Shaulian


For passing simply types, the data to post must take the form of a name value pair with the name portion being an empty string. So you need to make the Ajax call like so:

$.ajax({
  type: "POST",
  url: "/api/values",
  data: { "": list },
  dataType: "json",
  success: function() { alert("it worked!"); },
  failure: function() { alert("not working..."); }
});

Additionally, on your Web API action, annotate it w/ the [FromBody] attribute. Something like:

public void Post([FromBody]string[] values)

That should do the trick.

like image 28
Mark Berryman Avatar answered Nov 13 '22 11:11

Mark Berryman