Hi I'm trying to process an array in a php class by passing it through ajax.
I have read various tutorials that suggest using the JSON.stringify directive before the ajax call, but in doing so when I go to print an array, I saw that it is encoded as a string ... How can I process it as an php array instead?
This is the code I used: in js file:
let pw_script_vars = cc_object.ajax_url;
let pw_script_nonce = cc_object.itemNonce;
let lista_carte_finali =[18, 4, 12, 7, 16, 2, 3, 9, 1, 8];
let jsonString = JSON.stringify(lista_carte_finali);
function crea_tasto() {
$.ajax({
type: 'POST',
url: pw_script_vars,
cache: false,
data: {
action: 'carica_carte',
nonce: pw_script_nonce,
dati_carte: {dati_carte:jsonString}
},
success: function (dati_carte) {
console.log(dati_carte);
},
error: function () {
console.log('nessun dato');
}
});
}
$('#start_game').on('click', function () {
crea_tasto();
})
mentre lato server ho :
public function carica_carte() {
if (isset($_REQUEST)) {
foreach($_REQUEST['dati_carte'] as $d){
echo $d;
}
}
die();
}