Javascript 未捕获类型错误:无法读取属性';HotelInfo';未定义的 //对api的ajax调用 jQuery(文档).ready(函数(){ jQuery.ajax({ url:“http://localhost:8080/activitiesWithRealData?location=%22SEA%22&startDate=%2205-14-16%22&结束日期=%2205-16-16%22&主题=%22食物%22“, 数据类型:“JSON”,类型:“GET”, 成功:功能(数据) var模型; 如果(数据){ viewModel=新的dealsPageModel(数据); var idList=“”; 对于(var i=0;i
您可以循环查看Javascript 未捕获类型错误:无法读取属性';HotelInfo';未定义的 //对api的ajax调用 jQuery(文档).ready(函数(){ jQuery.ajax({ url:“http://localhost:8080/activitiesWithRealData?location=%22SEA%22&startDate=%2205-14-16%22&结束日期=%2205-16-16%22&主题=%22食物%22“, 数据类型:“JSON”,类型:“GET”, 成功:功能(数据) var模型; 如果(数据){ viewModel=新的dealsPageModel(数据); var idList=“”; 对于(var i=0;i,javascript,jquery,ajax,for-loop,typeerror,Javascript,Jquery,Ajax,For Loop,Typeerror,您可以循环查看数据.HotelInfoList.HotelInfo,但对HotelInfoList.HotelInfo[i]进行操作。ThumbnailUrl。开头的数据。 另外,将数据放入jQuery.get中的回调函数中: //an ajax call to the api jQuery(document).ready(function() { jQuery.ajax({ url:"http://localhost:8080/activitie
数据.HotelInfoList.HotelInfo
,但对HotelInfoList.HotelInfo[i]进行操作。ThumbnailUrl
。开头的数据。
另外,将数据
放入jQuery.get中的回调函数中:
//an ajax call to the api
jQuery(document).ready(function() {
jQuery.ajax({
url:"http://localhost:8080/activitiesWithRealData?location=%22SEA%22&startDate=%2205-14-16%22&endDate=%2205-16-16%22&theme=%22food%22",
dataType: 'JSON', type: 'GET',
success: function (data)
var viewModel;
if(data) {
viewModel = new dealsPageModel(data);
var idList = "";
for (var i = 0; i< data.packageDeal.length; i++)
{
if (i == data.packageDeal.length -1)
{ idList += data.packageDeal[i].hotelId;
}
else
{idList += data.packageDeal[i].hotelId + ',';
}
}
var searchUrl = "http://terminal2.expedia.com/x/hotels?hotelids=" + idList + "&apikey=6weV4ksGIJ5eQhd58o2XTDwVo35lZf2S";
//another call to another api to return hotel specific info
jQuery.get(searchUrl, function ( )
{
for(var i=0; i<viewModel.dealList.length; i++)
{
var hotelId = viewModel.dealList[i].hotelId;
for(var i=0; i<data.HotelInfoList.HotelInfo.length; i++)
{
var url = HotelInfoList.HotelInfo[i].ThumbnailUrl;
var name = HotelInfoList.HotelInfo[i].Name;
}
// Get the hotelid from the current deal
// Loop through the hotelinfolist.hotelInfo and find out the url for the hotel idList
//Loop through the hotelinfolist.hotelInfo and find out the name for the hotel
viewModel.dealList.push(new deal(data.packageDeal[i], url, name));
}
ko.applyBindings(viewModel);
});
}
}
})
});
您的数据在数据中。HotelFoliList不在HotelFoliList中
你的循环应该是这样的
jQuery.get(searchUrl, function(data){
// …
for(var i=0;i这里是我的问题:我正在尝试循环浏览HotelFolist,但是我得到了错误:Uncaught TypeError:无法读取未定义的属性'HotelInfo'。请尽快通知。我想您必须在这里接收'data'jQuery.get(searchUrl,function()
您可以添加响应数据的结构吗?这意味着您的对象HotelFolist
缺少名为HotelInfo
的属性。您希望它在那里,但我保证如果您使用console.log,它不会在那里。谢谢!错误已解决,但我们现在得到另一个: 未捕获的TypeError:无法读取line viewModel.dealList.push(new deal(data.packageDeal[i],url,name))中未定义的属性“10”@Vick我强烈建议您缩进代码。您早就注意到这一行在for
循环之外,I
此时等于data.HotelInfoList.HotelInfo.length
。请不要在注释中提出后续问题。请先尝试自己解决,然后再提出新问题如果你真的不能自己修好它,就继续。
for(var i=0; i<data.HotelInfoList.HotelInfo.length; i++)
{
var url = data.HotelInfoList.HotelInfo[i].ThumbnailUrl;
var name = data.HotelInfoList.HotelInfo[i].Name;
}