POST (PHP) with JSON

0

Well, I researched an answer and did not find it so I decided to ask here. I was doing a login system with JSON and PHP that should send a POST through JSON however the variable "_POST" always returns empty, prints:

This is the JSON that it sends to the "login.php" file.

Andthisiswhatreturns(Ps.:Iusedan"echo json_encode ($ _ POST)" in the file "login.php" and it returns "[]", however when the value is changed it is also displayed in the Firebug "Reply" which indicates that it is sending to the correct link. Does anyone understand this?

    
asked by anonymous 22.03.2015 / 15:20

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" , which 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');

then:

$data = json_decode($bodyRequest, true);

Now you can see the result:

var_dump($data);

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

    
17.10.2015 / 18:51