10

I want to POST an empty javascript array [] to webAPI and have it create an empty list of integers. I also want it so if I post javascript null to webAPI that it assigns null to the list of integers.

JS:

var intArray = [];
$.ajax({
 type: 'POST',
 url: '/api/ListOfInts',
 data: {'' : intArray},
 dataType: 'json'
});

c# webapi

[HttpPost]
public void ListOfInts([FromBody]List<int> input)

Problem 1) Jquery refuses to send data {'' : []} as the post payload. As soon as I add something in the array it works such as {'' : [1,2,3]}

Problem 2) Passing empty js array to controller gives null Based on what i read even if I do get it to post an empty array, it will initialize the list as null. Discussed solutions have it so that the list is always initializes as empty but I don't want that. I want it to be null in some case (when null/undefined is sent to it) and when [] is sent it should initialize as empty.

Edit: See https://www.asp.net/web-api/overview/advanced/sending-html-form-data-part-1 about why I am using {'' : []}

asked Jun 7, 2016 at 20:28
3
  • Have you tried to send just data: intArray ? Commented Jun 8, 2016 at 5:34
  • Yes, it doesnt work at all. WebAPI doesnt know how to handle it Commented Jun 9, 2016 at 14:58
  • In WebApi, differentiating between null and empty in a request can be difficult. If you are using those to somehow set some sort of "state" in your application, I would rethink how you are doing it. Possibly sending some sort of flag or something that will properly initialize your list on the server. Commented Jun 17, 2016 at 14:56

3 Answers 3

25
+25

Use JSON.stringify(intArray) and contentType: 'application/json' works for me:

$.ajax({
 url: "/api/values",
 method: "POST",
 contentType: 'application/json',
 data: JSON.stringify([])
});

enter image description here

$.ajax({
 url: "/api/values",
 method: "POST",
 contentType: 'application/json',
 data: null
});

enter image description here

$.ajax({
 url: "/api/values",
 method: "POST",
 contentType: 'application/json',
 data: JSON.stringify([1, 2, 3])
});

enter image description here

answered Jun 17, 2016 at 20:36

Comments

2
data: {'' : intArray},

a blank key name is not allowed in JSON.

Just send the array itself.

data: intArray,
answered Jun 7, 2016 at 20:34

2 Comments

This is required by webAPI when sending a valuetype
According to this question and answer, empty keys are allowed.
1

1. Use JSON.stringify() to convert javascript object to JSON string.

2. Use contentType: 'application/json' as it is correct MIME media type for JSON. What is the correct JSON content type?

3 dataType is not necessary

data: JSON.stringify(intArray), contentType: 'application/json'

answered Jun 20, 2016 at 12:47

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.