Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send a boolean value in jQuery ajax data

I'm sending some data in an Ajax call. One of the values is a boolean set to FALSE. It is always evaluated as TRUE in the PHP script called by the Ajax. Any ideas?

$.ajax({
    type: "POST",
    data: {photo_id: photo_id, 
           vote: 1, 
           undo_vote: false},   // This is the important boolean!
    url: "../../build/ajaxes/vote.php",
    success: function(data){
        console.log(data);
    }
}); 

In vote.php, the script that is called in the above Ajax, I check the boolean value:

if ($_POST['undo_vote'] == true) {
    Photo::undo_vote($_POST['photo_id']);
} else {
    Photo::vote($_POST['photo_id'], $_POST['vote']);
}

But the $_POST['undo_vote'] == true condition is ALWAYS met.

like image 692
Don P Avatar asked Feb 05 '13 20:02

Don P


Video Answer


1 Answers

You can use JSON.stringify() to send request data:

data : JSON.stringify(json)

and decode it on server:

$data = json_decode($_POST);

like image 197
Ihor Smirnov Avatar answered Oct 17 '22 07:10

Ihor Smirnov