: Getting error "Uncaught SyntaxError: Unexpected token :" on .JSON file When i Link a JSON file to an HTML file i get this error, "Uncaught SyntaxError: Unexpected token :" My JSON file looks
When i Link a JSON file to an HTML file i get this error, "Uncaught SyntaxError: Unexpected token :"
My JSON file looks like this:
{
"Names": [
{
"id": 1,
"name": "Test Name 1"
},
{
"id": 2,
"name": "Test Name 2"
}
]
}
More posts by @Cooney921
1 Comments
Sorted by latest first Latest Oldest Best
When i Link a JSON file to an HTML file
HTML provides no real standard mechanism for doing that.
The error message you are getting suggests that you are attempting:
<script src="myjson.json"></script>
JSON is a data format, not a JavaScript program, so you can't use a script element to source it.
The usual approach is to use the XMLHttpRequest object from JavaScript.
<script>
var xhr = new XMLHttpRequest();
xhr.open("GET", "myjson.json");
xhr.addEventListener('load', processJSON);
xhr.send();
function processJSON(event) {
var json = this.responseText;
var obj = JSON.parse(json);
// and do something with obj here
}
</script>
Terms of Use Create Support ticket Your support tickets Stock Market News! © vmapp.org2024 All Rights reserved.