Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

why request body is empty when using fetch

			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: #request #body #empty #fetch
ADD COMMENT
Topic
Name
5+6 =