未定义data.length

时间:2019-06-09 16:19:06

标签: javascript jquery

我想获取数据的长度,当我尝试console.log(data)时,我得到了所有数据,但console.log(data.length)未定义。

function tampilesai(idsoal){
$.ajax({
    url: "soalesai/baca/"+idsoal,
    type: "POST",
    dataType: "JSON",
    success: function(data){
        console.log(data); //here <=================
        console.log(data.length); //here <=============

        var html = "";   
        for (i = 0; i < data.length; ++i) {     
            html += "<tr>" +
                "<td class='isitbl'>" + data[i].idsoal + "</td>" +
                "<td class='isitbl' style='width:20px;'>" + data[i].noesai + "</td>" +
                "<td class='isitbl' style='width:200px;'>" + data[i].matakuliah + "</td>" +
                "<td>" + data[i].isiesai + "</td>" +
                "<td class='isitbl'><button class='btn btn-danger btn-block' id='hapus' data[i]-id='" + data[i].idsoal + "'>" +
                "<span class='icon-trash'></span> Hapus</button>" +
                "</td>" +
                "</tr>";
        }
        $("tbody#tblesai").html(html);
    }
})

我需要data.length进行循环并填充我的表。它的数据日志

{idesai: "90", idsoal: "1", noesai: "1", matakuliah: "1", isiesai: "<p>asdd</p>"}

此图像浏览器

enter image description here

3 个答案:

答案 0 :(得分:1)

如果数据是对象,则需要使用Object.keys(data).length来获取数据对象的长度。

答案 1 :(得分:0)

尝试以下操作... 将结果数据转换为JSON对象。虽然返回类型被明确声明为JSON,但是某些版本的jQuery可能不会自动解析它。我在2.x版本中遇到了这样的错误

    var data = $.parseJSON(data);
    // console.log(data.length) *should return 1
    var html = "";   
    $.each(data, function(c) {     
        html += "<tr>" +
        "<td class='isitbl'>" + c.idsoal + "</td>" +
        "<td class='isitbl' style='width:20px;'>" + c.noesai + "</td>" +
        "<td class='isitbl' style='width:200px;'>" + c.matakuliah + "</td>" +
        "<td>" + c.isiesai + "</td>" +
        "<td class='isitbl'><button class='btn btn-danger btn-block' id='hapus' data[i]-id='" + c.idsoal + "'>" +
        "<span class='icon-trash'></span> Hapus</button>" +
        "</td>" +
        "</tr>";
    });
    $("tbody#tblesai").html(html);

我还没有测试过,但是我认为它将给您一个替代的想法。

答案 2 :(得分:0)

完成..

function tampilesai(idsoal){
$.ajax({
    url: "soalesai/ambil/"+idsoal,
    type: "POST",
    dataType: "JSON",
    success: function(data){
        var html = "";
        for(i=0;i<data.length;i++){
            html += "<tr>" +
                "<td class='isitbl'>" + data[i].idsoal + "</td>" +
                "<td class='isitbl' style='width:20px;'>" + data[i].noesai + "</td>" +
                "<td class='isitbl' style='width:200px;'>" + data[i].matakuliah + "</td>" +
                "<td>" + data[i].isiesai + "</td>" +
                "<td class='isitbl'><button class='btn btn-danger btn-block' id='hapus' data-id='" + data[i].idsoal + "'>" +
                "<span class='icon-trash'></span> Hapus</button>" +
                "</td>" +
                "</tr>";
        }
        $("tbody#tblesai").html(html);
    }
})

}

我更改控制器

public function ambil($idsoal){
    echo json_encode(
            $this->pertanyaan_model
                ->ambilsoal($idsoal)->result());
}

和我的模特。

public function ambilsoal($idsoal){
    $query = $this->db
                ->where("idsoal",$idsoal)
                ->get("tblsoalesai");
    return $query;
}

及其作品,谢谢大家尝试帮助我..爱你