PHP - file_get_contents('php://input') is the only way to read contenttype=application/json request?

Asked

Viewed 1,186 times

1

I have the following Jquery Ajax method

        function postRefreshJson(_action, jsonModel)
        {
            $.ajax({
                url: _action,
                method: 'POST',
                beforeSend: function (xhr) {
                    $("body").css("cursor", "progress");
                },
                contentType: 'application/json',
                data: JSON.stringify(jsonModel),
                success: function (r) {
                    console.log("success: " + r);
                    alert(r);
                },
                error: function (e) {
                    console.log("Erro: " + e);
                    alert("Erro: " + e.responseJSON.errorMessage);
                    $("body").css("cursor", "default");
                }
            });
        }

Which I call something like this:

        var jsonModel = new Object();
        jsonModel.id = id;
        jsonModel.text = text;

        postRefreshJson("algumaUrl.php", jsonModel);

The only way I could find to receive the contents of the above request was by using

 $jsonObj = json_decode(file_get_contents('php://input'), true);

Isn’t there a different way to access the submitted content? I don’t know Using $_POST?

I’m using application/json, but actually it’s not very clear to me, what are the advantages of using it

Yes, I would also like to know the implications of using:

  json_decode(file_get_contents('php://input'), true);
  • 1

    cara.. take a look at this great response in the global OS: https://stackoverflow.com/a/8893792/6344251

No answers

Browser other questions tagged

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