javascript - jquery ajax 未捕获语法错误 : Unexpected token : while calling an api

标签 javascript jquery ajax

我试图从 Comicvine api 获取 json 响应,但收到以下错误。 Comicvine.gamespot.com/:1 未捕获的语法错误:意外的标记:

我在响应正文中看到格式化的 json 结果,但收到上面的控制台错误。

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

最佳答案

您需要设置format=jsonp而不是json

根据 comicvine.gamespot.com,jsonp 回调参数名称需要为 json_callback - 我通过访问 url https://comicvine 发现了这一点.gamespot.com/api/characters/?api_key=[your api key]&filter=gender:male,name:hawkeye&format=jsonp 在浏览器中,它告诉我缺少什么 - 非常友好的 API - 响应有一个错误值

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

并且不需要在 url 中使用 callback=? - 因为 jquery 添加了回调参数,并且它没有命名为回调

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

关于javascript - jquery ajax 未捕获语法错误 : Unexpected token : while calling an api,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44557211/

相关文章:

javascript - 基础选项卡 - 包括选项卡主 div 之外的内容

javascript - FlexiGrid - 在代码中设置网格宽度

javascript - 将 "active"类添加到带有取消 slider 的单独菜单

javascript - JQuery 下拉菜单是只读的但未禁用

javascript - Moment.js 根据特定日期(也是过去几年)获取周数

javascript - 计算图像的点击偏移量

php - jQuery datepicker,选择日期后添加一定天数

javascript - 附加所有字段形成 from to ajax 请求

javascript - AJAX:使用 jQuery 的 .ajax() 而不是 XMLHttpRequest 的优势?

Javascript私有(private)方法专用调用者返回未定义