1

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));

}
David
  • 208,112
  • 36
  • 198
  • 279
kev007
  • 21
  • 2
  • This isn't axios, this is fetch. Also, what's in `data` when you post it? In your browser's debugging tools, is any data included in the HTTP request? Why in the response do you ignore the response and return the originally posted data? Why return that at all when nothing uses it afterward? Please clarify the specific debugging you've observed. – David Oct 29 '21 at 13:13
  • I have that as an answer in the debugbar { "data": [ 5 ] }, – kev007 Oct 29 '21 at 13:26

1 Answers1

1

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); 
}
AndyNope
  • 427
  • 6
  • 12