在Javascript中使用JSON数组填充HTML

时间:2015-02-24 10:19:12

标签: javascript jquery html json cakephp

这个问题已在这里问了几次,但我还没有看到任何回复对我有用。

我有一段用cakePHP构建的代码,它成功地以JSON格式返回数据,但我还没有能够使用Javascript将这个JSON数据插入到HTML div中。

$('.book-click').click(function (e) {
    e.preventDefault();
    var getHotel = $(this).attr('hotel-id');

    $.ajax({
        type: "POST",
        url: "<?php echo $this->Html->URL(array('action' => 'postBook')); ?>",
        data: {
            hotel: getHotel
        },
        cache: false,
        dataTye: "json",
        success: function (JSONObject) {
            var myData = "";
            for (var key in JSONObject) {
                if (JSONObject.hasOwnProperty(key)) {
                    myData += JSONObject[key];
                }
            }
            /*console.log(myData);*/
            alert(myData[key]["hotelname"]); //Giving undifined in alert

        }
    });
});

这是我在浏览器控制台上记录数据时获得的JSON数据:

[
    {
        "id": "11",
        "hotelname": "Sheraton hotel",
        "hoteladdress": "Abule Ikeja",
        "state": "Lagos State",
        "lga": "Ikeja",
        "hotelphone": "65645545454",
        "hotelwebsite": "",
        "adminphone": "6565656565",
        "adminemail": "mail@hotel.com",
        "hotelemail": "",
        "facilities": ",,",
        "roomscategory": "Diplomatic",
        "standardrate": "150000",
        "leanrate": "",
        "aboutHotel": "",
        "requestStatus": "0",
        "createdOn": "1424360902",
        "updatedOn": "1424360902"
    }
]

如何成功将此JSON数据转换为HTML。像这样:

$('#myDiv').html(myData[key]["hotelname"]);

我试过这个,但它只是给了我Undefined

感谢任何帮助。

2 个答案:

答案 0 :(得分:4)

使用$.each()进行迭代:

success: function (JSONObject) {
    $.each(JSONObject, function(k,item){
       $('#myDiv').html(item.hotelname);
    });
}

&#13;
&#13;
var json = [
    {
        "id": "11",
        "hotelname": "Sheraton hotel",
        "hoteladdress": "Abule Ikeja",
        "state": "Lagos State",
        "lga": "Ikeja",
        "hotelphone": "65645545454",
        "hotelwebsite": "",
        "adminphone": "6565656565",
        "adminemail": "mail@hotel.com",
        "hotelemail": "",
        "facilities": ",,",
        "roomscategory": "Diplomatic",
        "standardrate": "150000",
        "leanrate": "",
        "aboutHotel": "",
        "requestStatus": "0",
        "createdOn": "1424360902",
        "updatedOn": "1424360902"
    }
]

$.each(json, function(i, item){
    $('body').append(item.hotelname);
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
&#13;
&#13;
&#13;


根据您的错误,您似乎没有有效的json或者是json字符串。所以在loopin之前你必须解析它:

success: function (JSONObject) {
    var data = $.parseJSON(JSONObject); // parse it
    $.each(data, function(k,item){
       $('#myDiv').html(item.hotelname);
    });
}

答案 1 :(得分:0)

var myData = "";
for (var key in JSONObject) {
   if (JSONObject.hasOwnProperty(key)) {
       myData += JSONObject[key];
   }
}

myData这里只是一个string。因此,调用myData[key]["hotelname"]将返回undefined,因为您正在尝试访问不存在的内容。

为什么不直接使用Ajax调用中的结果?