Search code examples
javascriptajaxmootoolshttp-post

Mootools ajax call removes data containing an empty array


in my data object for the ajax call, i've got something like this:

data: {
    someVariable: []
}

and when I start the ajax call, I can see in the POST data that someVariable is not being sent.

  1. is this a mootools problem, or is it JS-wide?
  2. how can i intentionally send an empty array?

Solution

  • While looking through a reported bug in the jquery bug tracker, they said the following:

    The problem is that there is no way to serialize an empty array in a way that makes sense in the x-www-form-urlencoded format. foo[bar]= could either mean empty array, empty string, empty object, or null/undefined value, depending upon how the backend chooses to interpret it. Since neither way of serialization is really “correct”, and most backends interpret this as “empty string”, and this breaks traditional mode in quite a bad way (as per the forum thread), I’m marking this to block 1.6 so it can be reverted back to the old behaviour.

    http://bugs.jquery.com/ticket/6481#comment:12

    Therefore,

    1. it is a limitation of javascript

    2. no. what you can do is make a placeholder for an empty array where it is wrapped in quotes, like '[]' and on the server side, you can interpret that as a new empty array.