Skip to content

Instantly share code, notes, and snippets.

@AlphaT7
Last active March 22, 2020 14:31
Show Gist options
  • Save AlphaT7/c10a8bd11e2d96b557a20b232b660660 to your computer and use it in GitHub Desktop.
Save AlphaT7/c10a8bd11e2d96b557a20b232b660660 to your computer and use it in GitHub Desktop.
PHP POST JSON JavaScript Fetch Example 2
const options = {
method: 'post',
headers: {
'Content-type': 'application/x-www-form-urlencoded; charset=UTF-8'
},
body: 'json={data: "data"}'
}
let url = "post.php";
fetch(url, options)
.then(response => {
return response.text(); // returns text that can used as html
//return response.json(); // returns json object
})
.then(html => {
document.querySelector("#html_element").innerHTML = html;
})
.then(() => {
// do something else;
})
.catch(function(err) {
console.log("Failed to fetch page: ", err);
});
.catch(err => {
console.error('Request failed', err)
})
<?php
$data = json_decode($_POST['json']);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment