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/

相关文章:

jquery - JQ加载窗口功能不起作用

JavaScript : download file from server : open file dialog to choose folder to save file

javascript - PHP Ajax 适用于 get,但不适用于 post

JavaScript 变量对象

javascript - jQuery 自动完成 : How to obtain HTML item's "id" attribute?

javascript - 从 Android native 浏览器上传时文件类型为空字符串

javascript - PHP 和 AJAX 消息框在用户滚动上滚动?

javascript - 调试 JavaScript - 需要插件解决方案建议

javascript - knockout 检查变量是否返回 true 和 false

php - 来自php的Ajax响应,在div内回显span?