1

The code is in coffeescript I am trying to read a json file using the code below (cross domain)

 $.ajax
    url: 'https://canttypecompanyurlhere/books.json'
    dataType: 'JSONP'
    jsonpCallback: 'callback'
    type: 'GET'
    success: (data) ->
      console.log (data)
    error: () ->
      console.log('error')

And here's the json

{ "name": "book-1", "author": "someone smart" }

What am I doing wrong? I cannot get past the error "uncaught SyntaxError: Unexpected token :"

Please help

blurfus
  • 13,485
  • 8
  • 55
  • 61
  • Where specifically is the syntax error? Who is complaining? Are you trying to run CoffeeScript using a JavaScript interpreter? – mu is too short Dec 01 '16 at 23:25
  • I am just trying to read that json file using the code above. There are no syntax errors in code above. When the code execution finishes ajax call it fails with that error in the title. "Unexpected token : " –  Dec 02 '16 at 00:33
  • Are you sure you want to make a `JSONP` request and not a `JSON` request, as what you showed as your payload isn't valid `JSONP` (read up on it [here](http://stackoverflow.com/questions/3839966/can-anyone-explain-what-jsonp-is-in-layman-terms)) – caffeinated.tech Dec 02 '16 at 10:03

1 Answers1

2

You're missing both the brackets to contain the $.ajax function parameters, as well as the braces to indicate the beginning and end of the data object, not to mention the commas at the end of each line of the object's definition. It should look like this:

    $.ajax ({
      url: 'https://canttypecompanyurlhere/books.json',
      dataType: 'JSONP',
      jsonpCallback: 'callback',
      type: 'GET',
      success: (data) -> {
        console.log (data);
      },
      error: () -> {
        console.log('error');
      }
    });
Ryan
  • 91
  • 5