Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
hb8
5,660 Pointsfetch data from a static json file
Hello, i'm trying to fetch data from a static json file uploaded to a web host from my localhost mamp. why am i getting this error message in the console "Uncaught SyntaxError: Unexpected token :" this is my code
the static testjson.json file on mytestsite.com:
{
"name" : "hello world"
}
My jQuery script on localhost mamp
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<script>
var url = "http://mytestsite.com/testjson.json?jsoncallback=?";
$.getJSON(url, function(response){
console.log(response);
})
</script>
</body>
</html>
6 Answers
james south
Front End Web Development Techdegree Graduate 33,271 Pointsi think the json is mal-formed, try putting name in quotes to make it a string, which i believe is required
{
"name": "hello world"
}
hb8
5,660 PointsHello James. Forgot the quotes in the example, but still not working. Console shows "Uncaught SyntaxError: Unexpected token :"
james south
Front End Web Development Techdegree Graduate 33,271 Pointswhat line? is it telling you which colon it isn't expecting?
hb8
5,660 Pointsconsole is referring to ?jsoncallback=?jsoncallback=jQuery22404344466194021004_1523876721406&_=1523876721407
Line 2 in the json file
james south
Front End Web Development Techdegree Graduate 33,271 Pointsgoogle jsoncallback, it is asking for a callback function to return JSONP, get rid of it to return JSON. see this
https://stackoverflow.com/questions/14313365/whats-the-use-of-jsoncallback-argument-in-getjson
hb8
5,660 PointsThanks for the help :) i'll look into it