Mobile app version of vmapp.org
Login or Join
Cooney921

: 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

@Cooney921

Posted in: #GoogleChrome

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"
}
]
}

10.01% popularity Vote Up Vote Down


Login to follow query

More posts by @Cooney921

1 Comments

Sorted by latest first Latest Oldest Best

 

@Ravi8258870

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>

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme