javascript - 我如何显示我的 table ?

标签 javascript php jquery ajax codeigniter

这是我的 index.php View ,我的 table 放置在其中

<table class="striped">
              <thead>
                <tr>
                    <th>Id</th>
                    <th>Name</th>
                    <th>Address</th>
                    <th>Created at</th>
                </tr>
              </thead>

              <tbody id="showdata">
                <!--<tr>
                  <td>1</td>
                  <td>Alvin</td>
                  <td>Eclair</td>
                  <td>$0.87</td>
                  <td>
                    <a href="javascript:;" class="waves-effect waves-light btn">Edit</a>
                    <a href="javascript:;" class="waves-effect waves-light btn">Delete</a>
                  </td>
                </tr>-->
              </tbody>
</table>

这是我的 ajax 脚本,放在index.php

function showAllEmployee() {
      $.ajax({
        type: 'ajax',
        url: '<?php echo base_url(); ?>index.php/Employee/showAllEmployee',
        async: false,
        dataType: 'json',
        success: function(data) {
          //console.log(data);
          var html = '';
          var i;
          for (i=0; i<data.length; i++) {
            html += '<tr>'+
                  '<td>'+data[i].emp_id+'</td>'+
                  '<td>'+data[i].name+'</td>'+
                  '<td>'+data[i].address+'</td>'+
                  '<td>'+data[i].created_at+'</td>'+
                  '<td>'+
                    '<a href="javascript:;" class="waves-effect waves-light btn" style="margin-right: 10px;" data="'+data[i].emp_id+'">Edit</a>'+
                    '<a href="javascript:;" class="waves-effect waves-light btn" data="'+data[i].emp_id+'">Delete</a>'+
                  '</td>'+
                '</tr>';
          }
          $('#showdata').html(html);
        },
        error: function() {
          alert('Could not get data from database');
        }

      });
    }

这就是我的员工 Controller 上的内容

public function showAllEmployee()
    {
        $result = $this->em->showAllEmployee();
        echo json_encode($result);
    }

这是我的模型

public function showAllEmployee()
    {
        $this->db->order_by('created_at', 'desc');
        $query = $this->db->get('tbl_employees');
        if($query->num_rows() > 0) {
            return $query->result();
        } else {
            return false;
        }
    }

每当我刷新页面时,数据都不会显示,而是遇到错误,无法从数据库获取数据,这是我在 ajax 脚本上设置的条件,可能是错误的,请帮助

最佳答案

在 Controller 中将 header 设置为 JSON 类型 将您的 Controller 更改为

public function showAllEmployee(){
    $result = $this->em->showAllEmployee();
    $this->output
        ->set_content_type('application/json')
        ->set_output(json_encode($result));
}

关于javascript - 我如何显示我的 table ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45072588/

相关文章:

php - 如何在不重复代码的情况下调用多个 AJAX 函数(到 PHP)

php - 我们是否必须设计 config.php、database.php 和通用 php 文件,或者我们可以从 php 库使用它

javascript - 尝试使用大气中的 jquery 插件对 meteor 表单提交进行表单验证

javascript - jquery 中的实时和触发

javascript - 如何根据 "li"结果创建 "fetch"s

asp.net - 如何确定 JavaScript 中 RadioButtonList 的 SelectedValue?

php - jQuery POST 多线程延迟

php - 使用 volley 和 php 将 android 连接到 mysql 时禁止错误 403

JQuery 对话框 : Show Message in Dialog Box After Selecting Confirm Option

javascript - 如何实现D3圆环图?