Cross Domain JSONP request returns Uncaught SyntaxError: Unexpected token :

Hi I am trying to get a data from an API.

I use an ajax call but then I always get an error message Uncaught SyntaxError: Unexpected token :

can anyone give me an example of how to do a correct jsonp call.

below is my code snippet. I am using coffeescript

get_shipping:=>
    shipper_id = @datapayload['general'][0]['shipper']
    origin = @datapayload['general'][0]['origin']
    destination = @datapayload['general'][0]['destination']
    if shipper_id == '001'
      expedition = 2
    if shipper_id == '002'
      expedition = 1
    if shipper_id == '003'
      expedition = 5
    if shipper_id == '004'
      expedition = 6
    api_code = 'my_api_code'
    @url = 'http://www.ongkoskirim.com/api/0.2/?id=' + api_code + '&o=' + origin + '&d=' + destination + '&c=' + expedition + '&callback=jsonhandler'
    $.getJSON @url, (data)=>
      alert jsonhandler
    $.ajax(
      url:@url
      headers:{'Access-Control-Allow-Origin': '*'}
      crossDomain: 'true'
      type:'GET'
      dataType:'jsonp'
      jsonpCallback:'jsonhandler'
      success:(data)=>
        console.log data
      error:=>
        console.log "error"
    )

any help is appreciated. Thx


What does your JSON look like? Sounds like it's malformed. Validate it here.

Also with jQuery's $.getJSON you don't need to specify a callback function, it will generate a random name for you and you can use the success() callback like normal.


With the $.getJSON URL string, append '?callback=dummyDummy' where dummyDummy is any string value that will be used as callback function name; you do not need to define dummyDummy anywhere else. Version of jQuery I had tested successfully with is 1.5.1.

链接地址: http://www.djcxy.com/p/46010.html

上一篇: JSONP返回“未捕获的SyntaxError:意外的令牌:”

下一篇: 跨域JSONP请求返回未捕获的SyntaxError:意外的标记: