Last active
March 24, 2025 13:58
-
-
Save Shakil-Shahadat/b8c910b897117cb367b203c928ff3cc2 to your computer and use it in GitHub Desktop.
✓ POST Request by Fetch
This file contains 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
<?php | |
header( 'Access-Control-Allow-Origin: *' ); | |
$received = file_get_contents( 'php://input' ); | |
echo $received; | |
// Ref: https://www.php.net/manual/en/reserved.variables.post.php#125651 |
This file contains 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', | |
headers: { 'Content-Type' : 'application/x-www-form-urlencoded' }, | |
body: 'data1=Hello World!&data2=Hello Universe!' | |
}) | |
.then( response => response.text() ) | |
.then( data => console.log( data ) ) | |
.catch( error => console.error( 'Error:', error ) ); | |
// Send data as json | |
fetch( 'post.php', | |
{ | |
method: 'POST', | |
headers: { 'Content-Type' : 'application/json' }, | |
body: JSON.stringify( todo ) | |
}) | |
.then( response => response.text() ) | |
.then( data => console.log( data ) ) | |
.catch( error => console.error( 'Error:', error ) ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment