0

When I use Content-Type: application/x-www-form-urlencoded to make a POST request using JavaScript

var url = "http://localhost:80/response.php";
var xhr = new XMLHttpRequest();

xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded") // Setting content-type

xhr.onreadystatechange = function() {
    if(xhr.readyState === 4){
        document.getElementById('div_response').innerHTML = xhr.responseText;
    }
};

var data = "message=Hello";
xhr.open("POST", url, true);
xhr.send(data);

POST request data in response.php is located in $_POST variable when Content-Type: application/x-www-form-urlencoded

if(isset($_POST['message']) && !empty($_POST['message'])) {
    echo $_POST['message'];
}

But, when I change the Content-Type to Content-Type: application/json. Where is my posted JSON data located in PHP. Because $_POST doesn't contain any data when Content-Type: application/json.

Phantom
  • 191
  • 1
  • 1
  • 6

0 Answers0