Javascript 返回未定义的JSON请求,不是错误 函数搜索游戏(查询){ //发出询问 $.ajax({ url:GamesSearchUrl, 键入:“获取”, 数据:{ 查询:查询, 资源:“游戏”, 格式:“jsonp”, 跨域:是的, 限额:16, 字段列表:“描述、名称、图像”, json_回调:“返回游戏”, 排序:“原始发布日期:描述” }, 数据类型:“jsonp” }); }; /*参考:http://stackoverflow.com/questions/22918573/giantbomb-api-work */ /*参考:http://www.giantbomb.com/forums/api-developers-3017/api-request-with-jsonp-uncaught-syntaxerror-1492639/ */ //返回结果时的回调 函数返回游戏(数据){ $('div#display').append('Found'+data.total+'results for'+data.query)); var games=data.game; $。每个(游戏、功能(索引、游戏){ $('div#display').append(''+game.name+''); $('div#display')。追加('
'+game.description+''); $('div#display')。追加(''); }); } 上面列出了函数,据我所知,JSONP数据应该传递给returnGames函数,但是页面上的输出是undefined的undefined results for undefined。控制台没有显示错误 我用的是巨型炸弹API。查询和GameSearchURL等变量在文档顶部定义为全局变量。函数中的console.log显示变量已正确传递给它。返回游戏顶部的console.log(数据)显示返回游戏顶部的console.log(数据)显示什么Javascript 返回未定义的JSON请求,不是错误 函数搜索游戏(查询){ //发出询问 $.ajax({ url:GamesSearchUrl, 键入:“获取”, 数据:{ 查询:查询, 资源:“游戏”, 格式:“jsonp”, 跨域:是的, 限额:16, 字段列表:“描述、名称、图像”, json_回调:“返回游戏”, 排序:“原始发布日期:描述” }, 数据类型:“jsonp” }); }; /*参考:http://stackoverflow.com/questions/22918573/giantbomb-api-work */ /*参考:http://www.giantbomb.com/forums/api-developers-3017/api-request-with-jsonp-uncaught-syntaxerror-1492639/ */ //返回结果时的回调 函数返回游戏(数据){ $('div#display').append('Found'+data.total+'results for'+data.query)); var games=data.game; $。每个(游戏、功能(索引、游戏){ $('div#display').append(''+game.name+''); $('div#display')。追加(',javascript,json,api,jsonp,Javascript,Json,Api,Jsonp,'+game.description+''); $('div#display')。追加(''); }); } 上面列出了函数,据我所知,JSONP数据应该传递给returnGames函数,但是页面上的输出是undefined的undefined results for undefined。控制台没有显示错误 我用的是巨型炸弹API。查询和GameSearchURL等变量在文档顶部定义为全局变量。函数中的console.log显示变量已正确传递给它。返回游戏顶部的console.log(数据)显
function searchGames(query) {
// send off the query
$.ajax({
url: GamesSearchUrl,
type : "get",
data: {
query : query,
resources: "game",
format: "jsonp",
crossDomain: true,
limit: 16,
field_list : "description,name,image",
json_callback : "returnGames",
sort : "original_release_date:desc"
},
dataType: "jsonp"
});
};
/* Reference: http://stackoverflow.com/questions/22918573/giantbomb-api-work */
/* Reference: http://www.giantbomb.com/forums/api-developers-3017/api-request-with-jsonp-uncaught-syntaxerror-1492639/ */
// callback for when we get back the results
function returnGames(data) {
$('div#display').append('Found ' + data.total + ' results for ' + data.query);
var games = data.game;
$.each(games, function(index, game) {
$('div#display').append('<h1>' + game.name + '</h1>');
$('div#display').append('<p>' + game.description + '</p>');
$('div#display').append('<img src="' + game.image + '" />');
});
}