Benjamin Liger Benjamin Liger - 16 days ago 5
jQuery Question

Ajax posting null data

I'm trying to echo a php variable from an Ajax request but the data is being returned as "null" and im not sure why. Here is my post request:

jQuery.ajax({
url:'/api/registerLoginViaFacebook.php',
data: {'test1':'test1','test2':'test2'},
type:'POST',
success: function(result) {
jQuery('header').html(result);
}
})


The data i'm posting here is just test data. As i'm having the same problem posting the real data. Here is the
registerLoginViaFacebook.php
file:

<?php

echo "<p>hey</p>";
echo "<p>".json_encode($data)."</p>";

?>


the html thats getting inserted in the
<header>
is heynull suggesting that the data i specified was never sent - at least that's what I make of it. I tried including
$_REQUEST['data'];
at the start of the file as per some suggestions I found on here but still no joy. Where have I gone wrong?

Answer

You need to set $data in your PHP script. Since you are sending the information via POST you can do this:

$data = $_POST;