POST (PHP) with JSON

Asked

Viewed 336 times

0

Well, I researched an answer and couldn’t find one, so I decided to ask here. I was making a Login system with JSON and PHP that should send a POST through JSON but the variable "_POST" always returns empty, prints:

This is the JSON it sends to the file "login.php". Isso é o JSON que ele envia.

And this is what it returns (Ps.: I used a "echo json_encode($_POST)" in the "login.php" file and it returns "[]", but when the value is changed it is also displayed in the "Reply" of Firebug which indicates that it is sending to the correct link. E isso é o que retorna Someone understands this?

  • Can you post the code? It would help us to better understand your problem.

  • What language is sending JSON to PHP? Javascript? If yes, pure Javascript, jQuery or other framework? Post the part of your code that makes the POST (be it in any language) and the part of PHP that receives it. Without it we can never help you.

1 answer

1

What may be happening is that you are sending a POST or PUT but without the header of Content-Type: "application/x-www-form-urlencoded", this indicates that it is a POST that the server is receiving.

So in order to get what was enveloped inside the HTTP header sent by the browser do the following in PHP:

$bodyRequest = file_get_contents('php://input');

and then:

$data = json_decode($bodyRequest, true);

now you can see the result:

var_dump($data);

Note:I would reply in a comment, but stackoverflow does not allow me to comment.

Browser other questions tagged

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