我试图从 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/