how to post json object array to a web api

How can I post a JSON array to a Web API? It’s working for single object.

This is what I’ve tried, but the controller seems to be returning 0 rather than the expected 3.

This is my JSON:

var sc = [{
              "ID": "5",
              "Patient_ID": "271655b8-c64d-4061-86fc-0d990935316a",
              "Table_ID": "Allergy_Trns",
              "Checksum": "-475090533",
              "LastModified": "2015-01-22T20:08:52.013"
          },
          {
              "ID": "5",
              "Patient_ID": "271655b8-c64d-4061-86fc-0d990935316a",
              "Table_ID": "Allergy_Trns",
              "Checksum": "-475090533",
              "LastModified": "2015-01-22T20:08:52.013"
          },
          {
              "ID": "5",
              "Patient_ID": "271655b8-c64d-4061-86fc-0d990935316a",
              "Table_ID": "Allergy_Trns",
              "Checksum": "-475090533",
              "LastModified": "2015-01-22T20:08:52.013"
          }];

AJAX call:
$.ajax({
           url: urlString,
           type: 'POST',
           data: sc,
           dataType: 'json',
           crossDomain: true,
           cache: false,
           success: function (data) { console.log(data); }
        });

Web API controller:
[HttpPost]
public string PostProducts([FromBody]List<SyncingControl> persons)
{
    return persons.Count.ToString(); // 0, expected 3
}

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

There is an error in the json Table_ID": "Allergy_Trns" should be "Table_ID": "Allergy_Trns".

Missing double quote.

Update

You need to make sure that you are sending your parameters as json as follows:

 $.ajax({
        url: urlString,
        type: 'POST',
        data: JSON.stringify(sc),
        dataType: 'json',
        contentType: 'application/json',
        crossDomain: true,
        cache: false,
        success: function (data) { console.log(data); }
    });

Notice the JSON.stringify(sc), @herbi is partly correct too about specifying a content type.

Screen grab

**Screen grab**

Method 2

You have to add the content-type header to the ajax request, so that WebAPI is able to understand the request and use the correct formatter to deserialize the data:

$.ajax({
           url: urlString,
           type: 'POST',
           data: sc,
           dataType: 'json',
           contentType: "application/json",
           crossDomain: true,
           cache: false,
           success: function (data) { console.log(data); }
        });

Method 3

You can set the Content-Type on beforeSend which will insure that your json data matches with your server object

$.ajax({
        beforeSend: function (xhr) {
            xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        },
           url: urlString,
           type: 'POST',
           data: sc,
           dataType: 'json',
           contentType: "application/json",
           crossDomain: true,
           cache: false,
           success: function (data) { console.log(data); }
        });


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x