Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery, AJAX, JSONP: how to actually send an array even if it's empty?

The problem is that you can't really send empty array. Have you tried to send an empty array manually? How would that uri look (note that it's the same reasoning for POST)?

/path?arr[]

This would result in a $_GET like this:

array (
 'arr' => array (
    0 => ''
  )
)

That's not really an empty array, is it? It's an array with a single element of an empty string. So what jQuery does, and I would agree that this is the correct way of handling it, is to not send anything at all.

This is actually really simple for you to check on the server. Just add an extra check whether the parameter exists or not, i.e:

$tabs = array();
if(isset($_POST['tab'])) {
  $tabs = $_POST['tab'];
}