Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

javascript fetch post sending empty body

			let datastring =  {
                submit: true,
				firstname: 'Kumar',
				lastname: 'D',
				email: 'kumar@test.com'
            }
            fetch('http://localhost.com/index_json_data.php', {
                method: 'POST',
                headers: {
                    'Accept': 'application/json, text/plain, */*',
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify(datastring),
            }).then(res => res.json())
              .then(res => console.log('after submit', res));

///// index_json_data.php PHP Code //////////
<?php
$json = file_get_contents('php://input');
// Converts it into a PHP object
$data = json_decode($json);
print_r($data); 
// Converts it into a PHP Array
$data = json_decode($json,true);
exit;
?>

 
PREVIOUS NEXT
Tagged: #javascript #fetch #post #sending #empty #body
ADD COMMENT
Topic
Name
1+8 =