When I pass json encoded value from PHP to Javascript and just console log it it returns me this:
{"id":"4","username":"muzikant346","coins":"675","avatar1":"1","avatar2":"0","avatar3":"0","avatar4":"0","avatar_selected":"0"}
This is a string and when I want to parse it in Javascript it returns an error:
eUncaught SyntaxError: Unexpected token in JSON at position 0
at JSON.parse (<anonymous>)
at Object.success (welcome.js:11)
at c (jquery-3.4.1.min.js:2)
at Object.fireWith [as resolveWith] (jquery-3.4.1.min.js:2)
at l (jquery-3.4.1.min.js:2)
at XMLHttpRequest.<anonymous> (jquery-3.4.1.min.js:2)
I don't understand why javascript can't parse this.
There is my PHP (fetch from database) code:
$sql = "SELECT * FROM serbian_values WHERE username = '$username'";
$result = mysqli_query($link, $sql);
if($result){
while($row = mysqli_fetch_assoc($result)){
$value[] = $row;
}
echo json_encode($value[0]);
}
JSON.parse()works fineU+FEFF : ZERO WIDTH NO-BREAK SPACE [BOM] {BOM, ZWNBSP}before the firstU+007B : LEFT CURLY BRACKET {left brace}: jsbin.com/xabezuwura/1/edit?html,js,outputobj='{"id":"4","username":"muzikant346","coins":"675","avatar1":"1","avatar2":"0","avatar3":"0","avatar4":"0","avatar_selected":"0"}'; console.log(JSON.parse(obj));