Posting null values via AJAX

Posted by nickf on Stack Overflow See other posts from Stack Overflow or by nickf
Published on 2010-06-15T04:35:53Z Indexed on 2010/06/15 4:42 UTC
Read the original article Hit count: 232

Filed under:
|
|
|

Using the jQuery $.post function, if you send a null value, it arrives at the server side as "null". Example:

Javascript:

$.post('test.php', { foo : null });

PHP:

var_dump($_POST['foo']); // string(4) "null"

I understand why this is so, but was wondering the best way to work around the limitation? Should you:

  1. Loop through all the variables in JS before you send them and replace with an empty string?
  2. Interpret "null" as null on the server side?
  3. Don't send the variable at all?
  4. Something else?

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about jQuery