How to pick up values from an object passed by fetch by PHP

Asked

Viewed 45 times

2

var dados = {
   id: 1234, 
   nome: "Teste", 
   tipo: "pagamento"
};


fetch('dados.php', {
    method: 'POST',
    body: JSON.stringify(dados),
    headers: {
        'Content-Type': 'application/json'
    }
})

I received this help of how to pass data through the fetch. My question is how to get these values in PHP. I tried print_r(dados), but it didn’t work out.

  • 1

    You are sending the data with Content-Type application/json. How PHP accepts, by default, data in format urlencoded, it would not be more prudent to send them in this format?

  • 2

    Behold here how to send fields using Content-Type urlencoded using the API fetch. If you are using PHP, there is no reason to send in JSON format, since urlencoded is supported natively by the superglobal $_POST.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.