javascript - foreach 数组的未定义结果 javascript

标签 javascript ajax codeigniter

我在销售事件日志中有如下表格

销售事件

id |    activity          |      userid     |     companyid         |     dateadded
 1   Set Meeting 3pm                2                 1               2019-12-26 10:29:59
 2   Send Proposal                  2                 1               2019-12-27 11:15:04
 3   Meeting                        3                 2               2019-12-27 13:01:13

我想通过模式弹出窗口根据客户端名称显示事件日志,但结果未定义。

这是js代码

 <script type="text/javascript">
 $('a[href="#myHistory"]').on('click',function(){

var id = $(this).attr('id');

$.ajax({
    type : "POST",
    url  : "<?php echo admin_url(). 'sales/get_history'; ?>",
    data : {id:id},
    success : function(data){
        var html = '';
        var i;
        for(i=0; i<data.length; i++){
            html += '<tr>'+
                        '<td>'+data[i].description+'</td>'+
                        '<td>'+data[i].dateadded+'</td>'+
                    '</tr>';
        }
        $('#show_data').html(html);
    }
})
})
</script>

这是我的 Controller

public function get_history(){
    $code=$this->input->post('id');
    $data=$this->sales_model->get_history_by_code($code);
    echo json_encode($data);
}

这是我的模型

function get_history_by_code($code)
{
    $abc=$this->db->query("SELECT * FROM tblsales_log WHERE companyid='$code'");
    if($abc->num_rows()>0){
        foreach ($abc->result() as $data) {
            $result=array(
                'description'      => $data->description,
                'dateadded'        => $data->dateadded,
            );
        }
    }
    return $result;
}

这是我的观点

  <div class="modal" id="myHistory" role="dialog">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
        <div class="modal-header">
            <h5 class="modal-title">Modal title</h5>
            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
            <span aria-hidden="true">&times;</span>
            </button>
        </div>
        <div class="modal-body">
            <table id="example" class="table table-striped table-bordered dt-responsive nowrap" style="width:100%">
                <thead>
                    <tr>
                        <th>Description</th>
                        <th>Added Time</th>
                    </tr>
                </thead>
                <tbody id="show_data">

                </tbody>
            </table>
        </div>
    </div>
</div>

当模态弹出时,仅显示未定义。我已经尝试过console.log,但结果仍然未定义。你知道我的代码错误在哪里吗?

谢谢

最佳答案

在 ajax 响应中,您必须解析 JSON。

$.ajax({
type : "POST",
url  : "<?php echo admin_url(). 'sales/get_history'; ?>",
data : {id:id},
success : function(data){

var data = JSON.parse(data);
  console.log(data);
    var html = '';
    var i;
    for(i=0; i<data.length; i++){
        html += '<tr>'+
                    '<td>'+data[i].description+'</td>'+
                    '<td>'+data[i].dateadded+'</td>'+
                '</tr>';
    }
    $('#show_data').html(html);
}

}) })

关于javascript - foreach 数组的未定义结果 javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59539626/

相关文章:

php - 没有根据where条件从数据库中获取正确的值

javascript - 使用 CodeIgniter 通过 AJAX 提交表单

javascript - 如何在 jQuery 中向动态下拉列表添加静态选项

javascript - 如何将 mCustomScrollbar 添加到由 javascript 生成的 div 中?

javascript - 未捕获的类型错误 : Cannot read property 'lat' of undefined when serializing leaflet data with $. 参数()

asp.net - 在线控件属性编辑器,类似于 Visual Studio 中的控件属性编辑器

php - 检查 API 中的强制参数

CRM 2011 HTML Web 资源中无法识别 Javascript 函数

javascript - 如何取消settimeOut的队列工作?

javascript - 如何使用 Angular 从 json 制作 View (表单)?