Im trying to fetch simple json text but my browser tells me that theres a syntax error which I cant find.
Heres my JS fetch function:
componentDidMount(){
fetch("http://localhost/ajax/getProfile.php")
.then((response) => {
if (!response.ok) {
// Before parsing (i.e. decoding) the JSON data,
// check for any errors.
// In case of an error, throw.
throw new Error("Something went wrong!");
}
return response.json(); // Parse the JSON data.
})
.then((data) => {
console.log(data);
})
.catch((error) => {
console.log(error);
});
}
And there is my php ajax php-code and output:
<?php
header("Access-Control-Allow-Origin: http://localhost:3000");
require_once "../php/dbh.php";
require_once "../php/functions.php";
session_start();
if(!(userExists($conn,$_SESSION['email'],$_SESSION['email']) === false)){
echo json_encode(userExists($conn,$_SESSION['email'],$_SESSION['email']));
} else if(!(userExists($conn,$_SESSION['username'],$_SESSION['username']) === false)){
echo json_encode(userExists($conn,$_SESSION['username'],$_SESSION['username']));
}
?>
Output:
{"userId":4,"username":"tt","userEmail":"[email protected]","userPwd":"$2y$10$Oo.HtuwOtauNoQ2Z3qETCObyn0HuNA71CgHdYXod8PEaG8RrASb4G"}