I have a simple set of HTML and JS files that both spectacularly fail, and I can't figure out why:
<html>
<head>
<script type="text/javascript" src="data.json"></script>
</head>
<body>
</body>
</html>
var data = '{"name" : "Phil Powell"}';
alert(data + ' ' + data.name + ' ' + data.type);
But every single time I try to open this up, I get the exact same error every single time:
{"name" : "Phil Powell"} undefined undefined
What am I doing wrong? All I want to do is parse an external JSON file, and I can't seem to do it.
Please help.
Thanks
undefinedhere is not an error, it's a value.undefinedas their value.scripttag. You need AJAX to read .json files. JSON is a textual data exchange format, it's not JavaScript.