0

I need to pass an array parameter to my ASP.NET backend. It looks like this:

$.ajax({ url: 'test', type: 'POST', data: { test: [1, 2] }})

And the generated form data is: test[]=1&test[]=2.

So far so good. But the problem occurs when I try to pass a null value inside an array:

$.ajax({ url: 'test', type: 'POST', data: { test: [1, null] }})

In this case for some reason generated form data looks like: test[]=1&test[1]=.

Please notice that second test segment has a numeric index. This cannot be parsed correctly on the backend: segment with an index is ignored, so I don't have my null value on the backend.

Any idea how to make it working?

2
  • Try to use { test: [1, 0] } or { test: [1, -1] } and convert back inside of API Commented Sep 1, 2021 at 18:38
  • @Serge Thanks, I'm already doing that but it's just a workaround, I would like to solve it in a cleaner way. Commented Sep 2, 2021 at 7:17

1 Answer 1

0

I solved it by using empty string instead of null:

$.ajax({ url: 'test', type: 'POST', data: { test: [1, ''] }})

In this case form data looks like this: test[]=1&test[]= and is correctly parsed on the backend to 1 and null.

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

Comments

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.