Published
- 1 min read
javascript fetch post sending empty body
The solution for this is noted below
javascript fetch post sending empty body
Solution
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;
?>
Try other methods by searching on the site. That is if this doesn’t work