Last active
May 15, 2024 08:51
-
-
Save AlphaT7/b0c05b274b0424e5975434fd746ee217 to your computer and use it in GitHub Desktop.
PHP POST JSON JavaScript Fetch Example 1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$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