I'm trying to create a Send function via Ajax with pure JavaScript, but PHP does not receive the data.
JavaScript is sending the 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 without success.
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 to return it.