I have googled the life out of the search engine and am unable to find a fix for my error 'Uncaught SyntaxError: Unexpected token ':' all I am trying to do is open JSON file from js folder. here is my javascript code using ajax:
function getData() {
console.log("about to get data");
$.ajax({
type: "GET",
crossDomain: true,
url: "js/data.json",
dataType: "jsonp",
// data:"{firstName:Rack}",
// accepts:'application/json',
//contentType: 'Content-Type:text/html',
// jsonpCallback: 'parseResponse',
success: function(response) {
console.log("Successful");
console.log(response);
},
beforeSend: function() {
console.log("before send: " + this.response);
}
});
}
and in my dashboard.html page, I call this via HTML button on click event approach.
<div class="container">
<div class="row">
<div class="col-md-8 offset-md-2">
<button type="button" class="btn btn-info" onclick="getData()">Get data</button>
<br />
<p></p>
</div>
</div>
</div>
The above does call as I hit the console.log line within getData() method. the json file format look like this:
{
"firstName": "Rack",
"lastName": "Jackon",
"gender": "man",
"age": 24,
"address": {
"streetAddress": "126",
"city": "San Jone",
"state": "CA",
"postalCode": "394221"
},
"phoneNumbers": [
{
"type": "home",
"number": "7383627627"
}
]
}
when inspecting the error message it keeps pointing to firstName : Rack line I can't seem to find any faults with the JSON file. I have tried different approaches as you can see from the comment outline all with failed attempt. if you change the dataType from jsonp to json it throw access to xmlhttprequest CORS policy error which I have also spent hours end trying to find a fix for and the closest I got was the above approach. Any support or direction would be very much appreciated am sure I am doing something stilly.