I have a JSon object in a js page. I want to receive this data in a php page.
Page: ajax.js:
var campi={0: {1: ' ', 2: ' ', 3: ' '}};
campi=JSON.stringify(campi); //thank you @Musa
$.ajax({
url: example_ajax_obj.ajaxurl,
type: 'POST',
data: {
'action': 'example_ajax_request',
'campi' : campi
},
success:function(data) {
console.log(campi);
},
error: function(errorThrown){
console.log(errorThrown);
}
});
Page: page.php
function example_ajax_request() {
$campi = $_POST['campi'];
echo "<script>console.log( 'Debug Objects: " . $campi . "' );</script>";
$json_data = json_decode($campi, true);
echo "<script>console.log( 'Debug Objects: " . $json_data . "' );</script>";
}
add_action( 'wp_ajax_example_ajax_request', 'example_ajax_request' );
add_action( 'wp_ajax_nopriv_example_ajax_request', 'example_ajax_request' );
add_action( 'wp_enqueue_scripts', 'example_ajax_request' );
Consolle shows me:
-Debug Objects: //due to echo "console.log( 'Debug Objects: " . $campi . "' );";
-Debug Objects: Array //due to echo "console.log( 'Debug Objects: " . $json_data . "' );";
{0: {1: ' ', 2: ' ', 3: ' '}}
I can't figure the reason why it doesn't work and I don't know how to debug. Thank you guys