Skip to content Skip to sidebar Skip to footer

Json.parse() Isn't Working

I don't understand why my json.parse() isn't working. The error is probably right before my eyes but I can't find it. Here is the code that I have:

Solution 1:

Do not create your JSON output yourself, use json_encode instead of string concatenation. A simple sample is below;

<?PHP$myResult = array();
    $myResult["key"] = "Value";
    $myResult["innerArray"] = array();
    $myResult["innerArray"]["key"] = "Value";
    $myResult["innerArray"]["isOk"] = true;
    $myResult["innerArray"]["amount"] = 100;
    echo json_encode($myResult);
?>

Working example of my sample https://ideone.com/4Kvvpj

Solution 2:

JSON.parse() is not working because json string is not proper. File_path is img\gallerij\Afbeelding-61.png but it should be img/gallerij/demo-image.jpg. Please change File_path everywhere in json string .

Click here for jsfiddle example.

File_path is getting generated from $outp .= '"File_path":"'. $rs["Foto_File"] . '",'; So replace \ with /.

Benefit: You get a path using JSON.parse() like img/gallerij/demo-image.jpg directly in client, so that this path can be used directly as src of DOM element.

Solution 3:

use JSON_encode(); for encoding your json value in php file

Post a Comment for "Json.parse() Isn't Working"