I'm having trouble with embedding json in my website.
This is html and javascript:
<!doctype html>
<html lang="us">
<head>
<meta charset="utf-8">
<title>jQuery UI Example Page</title>
<link href="css/custom-theme/jquery-ui-1.10.0.custom.css" rel="stylesheet">
<script src="js/jquery-1.9.0.js"></script>
<script src="js/jquery-ui-1.10.0.custom.js"></script>
<script>
function isJSON(str) {
try {
JSON.parse(str)
} catch (e) {
return false
}
return true
}
jQuery(function(){
<?
$arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);
?>
data = <?= json_encode($arr) ?>
if (isJSON(data)) {
json = jQuery.parseJSON(data)
console.log(json)
} else {
console.log("Not a json.")
console.log(data)
console.log(jQuery.parseJSON(data))
}
})
</script>
<body>
</body>
</html>
This outputs in console the following:
Not a json. filename.php:29
Object {a: 1, b: 2, c: 3, d: 4, e: 5} filename.php:30
Uncaught SyntaxError: Unexpected token o
Been a long day already and most probably (hopefully) I'm missing something obvious. I tried validate the generated json here: http://jsonlint.com/ and according to their site it was valid.
Thanks in advance!
<?php echo ... ?>
– BenM Feb 14 at 15:50