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

JavaScript

fetch 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

i think the json is mal-formed, try putting name in quotes to make it a string, which i believe is required

{
"name": "hello world"
}

Hello James. Forgot the quotes in the example, but still not working. Console shows "Uncaught SyntaxError: Unexpected token :"

what line? is it telling you which colon it isn't expecting?

console is referring to ?jsoncallback=?jsoncallback=jQuery22404344466194021004_1523876721406&_=1523876721407

Line 2 in the json file

google 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

Thanks for the help :) i'll look into it