Skip to content

Instantly share code, notes, and snippets.

@Shakil-Shahadat
Last active March 24, 2025 13:58
Show Gist options
  • Save Shakil-Shahadat/b8c910b897117cb367b203c928ff3cc2 to your computer and use it in GitHub Desktop.
Save Shakil-Shahadat/b8c910b897117cb367b203c928ff3cc2 to your computer and use it in GitHub Desktop.
✓ POST Request by Fetch
<?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
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