I have went through many solutions 1 but not getting json in java script getting an error.In my myfile.php file it contains
<?php
................
print json_encode($data, JSON_NUMERIC_CHECK);
mysql_close($con);
?>
<html>
<head>
<script type="text/javascript" src="jquery.min.js"></script>
<script type="text/javascript" src="example2.js"></script>
<script>
var json= loadJsonFromPHP(<?php echo json_encode($data) ?>);
//var json= <?php echo json_encode($data) ?>;
</script>
</head>
<body></body>
</html>
In my example2.js file
var loadJsonFromPHP = function(json) {
console.log(json);
}
function init(){
// init data
var json =loadJsonFromPHP(json);
}
getiing an error Cannot read property 'id' of undefined at console.log(json);,I am new to PHP ,and have no idea where am i getting wrong,
i have tried with .getJson in my js but getting error ReferenceError: $ is not defined
function init(){
// init data
var json=$.getJSON('http://localhost/myfile.php', function(data) {
console.log(data);
});
}
Stuck completely,any help,thanks.
$.parseJSON()
in your second example, cause the variable json is string at the moment. – Vahe Shadunts Dec 10 '13 at 13:57