I am attempting to convert a JSON string from my .php file using the eval() function, but it is not working. The browser console shows a SyntaxError: expected expression, got '<'...
However, when I comment out the line where eval() is used and instead use document.write(data), the string appears...
Here is the code snippet:
<html>
<head>
<script type='text/javascript' src='jquery.js'></script>
<script type='text/javascript'>
var go = function() {
$.get("testjson.php", function(data) {
var obj = eval("(" + data + ")");
document.write(obj.name + "<br />");
document.write(obj.date + "<br />");
});
}
</script>
</head>
<body>
<input type='button' value='go' onclick='go()' />
<body>
</html>
And here is the code of my testjson.php file:
<?php
$msg = array(
"name"=>"hi there Victor!",
"date"=>"Monday 21st Feb 2010"
);
$myMsg = json_encode($msg);
echo $myMsg;
?>
I am using the latest version of jQuery.