I have no idea how to get the "Hello World!" in PHP for the following Javascript codes.
I know I can use $_POST[''] if the content-type was "application/x-www-form-urlencoded", but not for "text/plain".
var xhr = new XMLHttpRequest();
xhr.open('POST', 'example.php', true);
xhr.setRequestHeader('Content-Type', 'text/plain');
xhr.send('Hello World!');
This PHP will read the raw data from the request body:
$data = file_get_contents('php://input');
Line 3:
xhr.setRequestHeader('Content-Type', 'text/plain');
isn't required as posting plain text will set the content type to text/plain;charset=UTF-8
http://www.w3.org/TR/XMLHttpRequest/#the-send-method
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With