I try to use the FETCH API to handover Data to a php Programm. But in the JS console i keep getting this error.
SyntaxError: Unexpected token '<', "
<br />
<b>"... is not valid JSON
const category = document.getElementById('genreSelect').value;
const js1 = '{"genre": "' + category + '"}';
const js2 = JSON.stringify(js1);
fetch("button.php", {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: { category: js2 }
})
.then(response => response.json())
.then(data => {
})
.catch(error => {
console.error('Error:', error);
});
<?php
header('Content-Type: application/json');
if(isset($_POST)){
$data = file_get_contents('php://input');
$kate = json_decode($data, true);
$kat = $kate[0];
$every = array();
echo json_encode($every);
I validated the JSON, but it was fine, so I ran out of possibilities left to do.