I am trying to send data from JavaScript with fetch to PHP but in PHP I get empty array when I do $_POST.
async function updatesProf(data) {
const dataUpdate = {
method:"POST",
headers: {
'Accept':'application/json',
'Content-Type':'application/json',
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
body: JSON.stringify({ data: data })
};
await fetch('synthese', dataUpdate)
.then(response => response.json()).then(res => data )
.catch(error => console.log("Error: "+error));
}
Classic Problem, if you send your $_Post with Javascript.
Try with:
$_POST = json_decode(file_get_contents('php://input'), true);
if(isset($_POST)){
var_dump($_POST);
}