Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In php, how do I get the text/plain value of send() method of XMLHttpRequest

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!');
like image 942
abc Avatar asked Dec 22 '22 09:12

abc


1 Answers

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

like image 166
Adria Avatar answered Dec 24 '22 02:12

Adria