Skip to content

Instantly share code, notes, and snippets.

@AlphaT7
Last active May 15, 2024 08:51
Show Gist options
  • Save AlphaT7/b0c05b274b0424e5975434fd746ee217 to your computer and use it in GitHub Desktop.
Save AlphaT7/b0c05b274b0424e5975434fd746ee217 to your computer and use it in GitHub Desktop.
PHP POST JSON JavaScript Fetch Example 1
fetch("post.php", {
method: "post",
mode: "cors",
headers: {
"Content-Type": "application/json"
},
body: {data: "data"}
})
.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);
});
$data = json_decode(trim(file_get_contents("php://input")));
// do something with $data;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment