我有一个表单,我收集数据,然后我将它们插入url字符串, 因此我得到像http://terminal2.expedia.com/x/mhotels/search?city=hersbruck&checkInDate=2016-12-01&checkOutDate=2016-12-03&room1=2&apikey=RZjKNligNc0QZJvV9HP1nbFm1jrghgLK这样的网址 此url直接在页面上返回有效的json
{
numberOfRoomsRequested: 1,
filterUnavailableHotelsRequested: false,
totalHotelCount: 94,
availableHotelCount: 60,
searchRegionId: "6124915",
searchRegionCity: "Hersbruck",
deepLinkUrl: "https://www.expedia.com/Hersbruck-Hotels.d6124915.Travel-Guide-Hotels",
hotelList: [
{
sortIndex: "0",
hotelId: "2813073",
name: "Md Hotel Restaurant Bauer",
localizedName: "Md Hotel Restaurant Bauer",
nonLocalizedName: "Md Hotel Restaurant Bauer",
address: "Martin Luther Str 16",
city: "Hersbruck",
........
我尝试从中获取一些数据来创建表,使用这样的代码:
$(document).ready(function(){
$('#submit-btn').click(function(s) {
$("form").submit(function (event) {
var place = (document.querySelectorAll('#place')[0].value);
var check_in = (document.querySelectorAll('#check-in')[0].value);
var check_out = (document.querySelectorAll('#check-out')[0].value);
var api_key = "RZjKNligNc0QZJvV9HP1nbFm1jrghgLK";
var url = "http://terminal2.expedia.com/x/mhotels/search?city=" + place + "&checkInDate=" + check_in + "&checkOutDate=" + check_out + "&room1=2&apikey=" + api_key + ""
$.getJSON(url, function (data){
var div = $("#result");
var table = '<table><tbody>';
for (i in data) {
var tr = '<tr><td>'+data[i].name+'</td><td>'+data[i].city+'</td></tr>'
table = table+tr;
}
table = table + "</tbody></table>";
div.html(table);
});
event.preventDefault();
});
});
});
但我收到了undefined
的表格
我做错了,请指出我。
答案 0 :(得分:0)
我猜你想要显示API返回的hotelList酒店。那么你的循环不会更像:
for (hotel in data.hotelList) {
var tr = '<tr><td>'+ hotel.name+'</td><td>'+ hotel.city+'</td></tr>'
table = table+tr;
}
为了清楚起见,我改变了我的酒店。
但是因为我无法看到整个JSON,所以我不知道我的猜测是否正确。