The standard jQuery .ajax() method uses the data property to create an x-www-form-urlencoded string to pass to the request body. Something like that
action=Flickr&get=getPublicPhotos
Therefore, your PHP script should not look for $_POST['data'] , but instead $_POST['action'] and $_POST['get'] .
If you want to send the raw JSON payload to PHP, do the following ...
Set the AJAX contentType to application/json and send the gated version of your JSON object as a data payload, for example
$.ajax({ url: '../phpincl/apiConnect.php', type: 'POST', contentType: 'application/json', data: JSON.stringify(flickrObj), dataType: 'json' })
Your PHP script will then read the data payload from the php://input stream, e.g.
$json = file_get_contents('php://input');
Then you can parse this into a PHP object or array ...
$dataObject = json_decode($json); $dataArray = json_decode($json, true);
And, if you just want to recall it back to the customer.
header('Content-type: application/json'); // unmodified echo $json; // or if you've made changes to say $dataArray echo json_encode($dataArray);
source share