简体   繁体   中英

$.ajax error uncaught SyntaxError: Unexpected token :

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

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');
      }
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM