I’m trying to make a simple API with PHP and a way to retrieve data from it in JavaScript but I keep getting the following error: SyntaxError: Unexpected token < in JSON at position 0
. The JSON is valid and I’m not sure what is causing the error…
I’ve tried setting headers and the data is encoded with json_encode()
so there’s not really any excuse for it to not be working.
API link: [removed by author]
PHP Code:
<?php include("../config/connect.php"); $method = $_SERVER['REQUEST_METHOD']; $request = explode('/', trim($_SERVER['PATH_INFO'],'/')); ob_clean(); header_remove(); header("Content-type: application/json; charset=utf-8"); if ($success) { http_response_code(200); } else { http_response_code(500); } // Error handling $username = filter_var(htmlentities($_GET['username'])); if (!$username) { $api = array("Error"=>"Error: No username specified."); echo json_encode($api); exit(); } $quesql = "SELECT * FROM signup WHERE username=:username"; $query = $conn->prepare($quesql); $query->bindParam(':username', $username, PDO::PARAM_STR); $query->execute(); while ($getinfo = $query->fetch(PDO::FETCH_ASSOC)) { $fetch_id = $getinfo['id']; $fetch_username = $getinfo['Username']; // And so forth... } if ($query->rowCount() == 0) { $api = array("Error"=>"Error: No such user."); echo json_encode($api); exit(); } $avatar = $fetch_avatarURL == "user.png" ? "https://comiq.eddyn.net/imgs/user_imgs/user.png" : $fetch_avatarURL; $occ = $fetch_occupation == NULL ? "none" : $fetch_occupation; $bio = $fetch_bio == NULL ? "none" : $fetch_bio; // And so forth... $api = array("username"=>$fetch_username, "joinDate"=>$fetch_joined, "avatarURL"=>$avatar, "occupation"=>$occ, "bio"=>$bio, "hometown"=>$hometown, "hobbies"=>$hobbies, "likes"=>$likes, "dislikes"=>$dislikes, "verified"=>$verified, "admin"=>$admin, "mod"=>$mod, "dev"=>$dev, "artist"=>$artist, "plus"=>$plus, "online"=>$online); echo json_encode($api); exit(); ?>
JS Code:
try { request('https://comiq.eddyn.net/api/profile?username=' + args[0], function (error, body) { var result = JSON.parse(body.body) const embed = new Discord.MessageEmbed() .setColor(colors.default) .setTitle(result.username) .addField("Occupation", result.occupation, true) .addField("Bio", result.bio, true) .addField("Hometown", result.hometown, true) .addField("Hobbies", result.hobbies, true) .addField("Likes", result.likes, true) .addField("Dislikes", result.dislikes, true) .addField("Verified", result.verified, true) .addField("Comiq Plus", result.plus, true) .addField("Online", result.online, true) .setImage(result.avatarURL) message.channel.send(embed) }) } catch (err) { message.channel.send(client.errors.genericError + err.stack).catch(); }
Advertisement
Answer
your response is JSON data.
So you should to remove the line of var result = JSON.parse(body.body)
.
Just use body
as result
.
try { request('https://comiq.eddyn.net/api/profile?username=' + args[0], function (error, body) { var result = body; const embed = new Discord.MessageEmbed() .setColor(colors.default) .setTitle(result.username) .addField("Occupation", result.occupation, true) .addField("Bio", result.bio, true) .addField("Hometown", result.hometown, true) .addField("Hobbies", result.hobbies, true) .addField("Likes", result.likes, true) .addField("Dislikes", result.dislikes, true) .addField("Verified", result.verified, true) .addField("Comiq Plus", result.plus, true) .addField("Online", result.online, true) .setImage(result.avatarURL) message.channel.send(embed) }) } catch (err) { message.channel.send(client.errors.genericError + err.stack).catch(); }