PHP does not receive POST Json

Asked

Viewed 994 times

1

I’m trying to create a sending function via Ajax with pure Javascript, but PHP does not receive the data.

Javascript is sending data correctly.

Javascript code:

    this._data = JSON.stringify(data);
    Object.freeze(this);

    function reqListener() {
        console.log(this.responseText);
    };

    let request = new XMLHttpRequest();
    request.onload = reqListener;;
    request.open('POST', '/api');
    request.setRequestHeader('Content-Type', 'application/json;');
    request.send(this._data);

I tried to use application/x-www-form-urlencoded also unsuccessful.

PHP code:

$jSon = array(); 
$getPost = file_get_contents('php://input');
$post = json_decode($getPost);
echo json_encode($jSon);    

The purpose of these codes is to send a JSON via Ajax and PHP returns itself.

  • Does it give an error message? If not, it seems to me that your PHP code doesn’t make much sense. You give echo in the variable $jSon, but it will always be an empty array. If you just want to echo the data, the correct would not be to pass the value of $post for the function json_encode? Something like echo json_encode($post)

  • @Andersoncarloswoss Either way it will be empty, because file_get_contents('php://input') is empty. This is my problem, the client shows that the data was sent, but back-end returns an empty array. The PHP code is just a test, from the moment it works I will start developing the application in s

  • And if you do var_dump($_POST), what is the return?

  • @Andersoncarloswoss He returns null also, from what I read on the help pages, the file_get_contents('php://input') takes the raw data, while the $_POST treats him. Because JSON is the $_POST will always return null.

2 answers

0

You have an almost functional example, except for two details (Object Freeze in javascript and use an empty variable in json_encode). Running the minimal example all runs as expected. See:

<script>
    var data = { "name":"John", "age":30, "city":"New York"};
    this._data = JSON.stringify(data);
    //Object.freeze(this);

    function reqListener() {
    console.log(this.responseText);
    };

    let request = new XMLHttpRequest();
    request.onload = reqListener;;
    request.open('POST', '/stackoverflow.php');
    request.setRequestHeader('Content-Type', 'application/json;');
    request.send(this._data);
</script>

Already in php:

<?php
$jSon = array(); 
$getPost = file_get_contents('php://input');
$jSon = json_decode($getPost);
echo json_encode($jSon);

Looking at the browser debugger you can see that the data sent is returned by the php script.

  • 2

    Can you explain why Object.freeze interfere in the solution? I could not understand, because in my view the object was not modified.

  • 1

    When the Object.Freeze method is executed it throws an error (says it cannot prevent the object from being modified). As I did not find it relevant to use it (according to the example). This error was interrupting the execution of the script so I removed it. Summarizing it doesn’t work with this object (from my context), but it would work with this. _data.

  • I understood, thank you. I also felt that there was no need for it there, but I just didn’t understand why to influence the solution. Generating the error and interrupting the script would explain this.

  • @Juven_v removed the Object.freeze and changed PHP according to your example, but for me only returns null. I’m using PHP 7.1 will have something to do?

-2

I switched: this. _data = JSON.stringify(date); By: var json_data = JSON.stringify(date);

It worked here

Browser other questions tagged

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