I have pscript.php file they have array $timeslots(). Array need a send to script.js used fetch(). How this do it? The files are in the same folder.
php:
$timeslots = array();
while ($cells = mysqli_fetch_assoc($result))
{
array_push($timeslots, $cells['rectime']);
}
js:
async function getResponse() {
let respons = fetch('pscript.php', {
method: 'get',
headers: { 'Content-Type': 'application/json' },
//body: JSON.stringify($timeslots)
})
let content = await respons.json();
console.log(content);
};
getResponse();
I tried send it with this code on php file
<script><?php echo json_encode($timeslots); ?></script>
and with it
echo json_encode($timeslots);
but html show me it
jsscript.js:7
Uncaught (in promise) TypeError: respons.json is not a function
at getResponse (jsscript.js:7:31)
at jsscript.js:11:1
"... is not valid JSON – naffi Mar 16 '23 at 10:48