gpt4 book ai didi

javascript - 使用 JSONP,我有 'ReferenceError: data not defined'

转载 作者:行者123 更新时间:2023-11-30 12:07:09 25 4
gpt4 key购买 nike

我在显示通过 JSONP 调用的 JSON 数据时遇到问题。我有一个复杂的 JSON 文件通过 URL 跨域传送,我是 JSON 和 ajax 的新手。我在 https://learn.jquery.com/ajax/working-with-jsonp/ 关注了一篇 JSONP 文章那很有用。现在,在关注这个问题 How do I iterate through this JSON object in jQuery? 之后,我正在尝试遍历 JSON 对象并在 HTML 页面中显示数据。而且我哪儿也不去。数据未定义,我不确定我做错了什么:

// Using LibCal and JSONP
$.ajax({
url: "https://api2.libcal.com/1.0/room_bookings_nickname/?iid=3356&group_id=12306&key=92a47e5c854dee620cca071648c3fc41",

// The name of the callback parameter, as specified by the YQL service
jsonp: "callback",

// Tell jQuery we're expecting JSONP
dataType: "jsonp",

// Tell LibCal what we want and that we want JSON
data: {
q: "select Room name,booked timeslots,booking name from LibCal room bookings",
format: "json"
},

// Work with the response
success: function( response ) {
console.log( response ); // server response
}
});

$.each(data.bookings, function(index, element) {
alert(element.timeslots.room_name);
});

我希望这对我们那里的更高级用户来说是一个明显的修复 :)

最佳答案

那是因为您的data 对象不存在。您所指的 data 键是您传递给 $.ajax 方法的对象上的一个键。

您需要在 success 回调中执行您的函数,这样它才有意义。

$.ajax({
...
// keep all your original stuff
...
success: function( response ) {
var data = response;
$.each(data.bookings, function(index, element) {
alert(element.timeslots.room_name);
});
}
});

关于javascript - 使用 JSONP,我有 'ReferenceError: data not defined',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34869147/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com