Skip to content

Instantly share code, notes, and snippets.

@ttristan
Last active March 23, 2018 08:19
Show Gist options
  • Save ttristan/c05b9a7352500f0a77b28bd916151772 to your computer and use it in GitHub Desktop.
Save ttristan/c05b9a7352500f0a77b28bd916151772 to your computer and use it in GitHub Desktop.
// JS
function request() {
const xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState == XMLHttpRequest.DONE ) {
console.log("xhr response", xhr.response)
if (xhr.status == 200) { // success
}
else if (xhr.status == 400) { // error 400 (bad request)
}
else { // error
}
}
};
const data = {
name: "Tristan",
value: "dev",
}
xhr.open("POST", "./search.php");
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.send(JSON.stringify(data));
}
// PHP
<?php
$data = json_decode(file_get_contents('php://input'), true);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment