Skip to content
Advertisement

jquery ajax Uncaught SyntaxError: Unexpected token : while calling an api

I am trying to get a json response from the comicvine api but am getting the following error. comicvine.gamespot.com/:1 Uncaught SyntaxError: Unexpected token :

I see my json result, formatted, in the response body but am getting the console error above.

export function getSeriesFromComicVine() {
  const url = "http://comicvine.gamespot.com/api/characters/?api_key=f18c6362ec6d4c0d7b6d550f36478c1cd6c04a49&filter=gender:male,name:hawkeye&format=json&callback=?";
  $.ajax({
    url: url,
    // data: {test: "test"},
    type: 'GET',
    crossDomain: true,
    jsonpCallback: 'callback',
    dataType: 'jsonp',
    jsonp: false,
    jsonpCallback: "myJsonMethod"
    success: function (data) {
    console.log(data);
     }
  });
}

Advertisement

Answer

You need to set format=jsonp not json

the jsonp callback parameter name needs to be json_callback according to comicvine.gamespot.com – I found this out by going to url https://comicvine.gamespot.com/api/characters/?api_key=[your api key]&filter=gender:male,name:hawkeye&format=jsonp in the browser, and it told me what was missing – very friendly API – the response had an error value

"'jsonp' format requires a 'json_callback' argument"

and no need for callback=? in the url – seeing as jquery adds the callback parameter and it isn’t named callback

function getSeriesFromComicVine() {
    const url = "https://comicvine.gamespot.com/api/characters/?api_key=[your api key]&filter=gender:male,name:hawkeye&format=jsonp";
    $.ajax({
        url: url,
        type: 'GET',
        dataType: 'jsonp',
        jsonp: "json_callback",
        success: function (data) {
            console.log(data);
        }
    });
}
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement