Skip to content

Instantly share code, notes, and snippets.

@digitalfun
Forked from AlphaT7/post.js
Created May 15, 2024 08:51
Show Gist options
  • Save digitalfun/377df849e9b7f4f6cd4bd096f27edd59 to your computer and use it in GitHub Desktop.
Save digitalfun/377df849e9b7f4f6cd4bd096f27edd59 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