使用 Codeigniter 和 Mysql 显示 ajax 结果时出错


Error in displaying the ajax result using Codeigniter and Mysql

我只需要一点关于显示我的表查询结果的帮助。当我检查来自我的 ajax 的响应时,我显示。但是当传递到视图时,它不会显示。这是我的代码:

控制器/用户.php

        <?php
        class User extends CI_Controller{
            public function __construct(){
                parent::__construct();
                $this->load->model('user_model');
            }
            public function index(){
                $this->load->view( 'index' );
            }
            public function read() {
                echo json_encode( $this->user_model->getAll() );
            }

        }
    ?>

型号/user_model.php

        <?php
        class User_Model extends CI_Model{
            public function __construct(){
                parent::__construct();
            }
            public function getAll(){
                $qGetAllData = $this->db->get('user');
                if($qGetAllData->num_rows() > 0){
                    return $qGetAllData->result();
                }else{
                    return array();
                }
            }
        }
    ?>
    View/index.php
        <html>
      <head>
        <title><?php echo $title; ?></title>
        <base href="<?php echo base_url(); ?>" />
        <link type="text/css" rel="stylesheet" href="css/smoothness/jquery-ui-1.8.2.custom.css" />
        <link type="text/css" rel="stylesheet" href="css/styles.css" />
      </head>
      <body>
        <table id="records"></table>
        <div id="tabs">
          <ul>
            <li><a href="#read">Read</a></li>
            <li><a href="#create">Create</a></li>
          </ul>
          <div id="read">
            <table id="records"></table>
          </div>
          <div id="create"></div>
        </div>


        <script type="text/javascript" src="js/jquery-1.4.2.min.js"></script>
        <script type="text/javascript" src="js/jquery-ui-1.8.2.min.js"></script>
        <script type="text/javascript" src="js/jquery-templ.js"></script>
        <script type="text/template" id="readTemplate">
          <tr>
            <td>${id}</td>                  //doesn't display ID
            <td>${name}</td>                //doesn't display Name
            <td>${email}</td>               //doesn't display EMial
          </tr>
        </script>
        <script type="text/javascript" src="js/myjs.js"></script>
      </body>
    </html>

Javascript/myjs.js

            $(document).ready(function(){
        $( '#tabs' ).tabs({
                fx: { height: 'toggle', opacity: 'toggle' }
            });
            $.ajax({
                url: 'index.php/user/read',
                datatype: 'json',
                success: function(response){
                    $('#readTemplate').render(response).appendTo('#records');
                }
            });
        });

这就是所有人。我只是不知道我的错误在哪里。

我认为您对渲染数据有问题。

请查看此链接以供参考。

请尝试以下代码。

Your html : View/index.php
<html>
  <head>
    <title><?php echo $title; ?></title>
    <base href="<?php echo base_url(); ?>" />
    <link type="text/css" rel="stylesheet" href="css/smoothness/jquery-ui-1.8.2.custom.css" />
    <link type="text/css" rel="stylesheet" href="css/styles.css" />
  </head>
  <body>
    <table id="records"></table>
    <div id="tabs">
      <ul>
        <li><a href="#read">Read</a></li>
        <li><a href="#create">Create</a></li>
      </ul>
      <div id="read">
        <table id="records"></table>
      </div>
      <div id="create"></div>
    </div>
    <script type="text/javascript" src="js/jquery-1.4.2.min.js"></script>
    <script type="text/javascript" src="js/jquery-ui-1.8.2.min.js"></script>
    <script type="text/javascript" src="js/jquery-templ.js"></script>
    <script type="text/javascript" src="js/myjs.js"></script>
  </body>
</html>
Your JS : js/myjs.js
var readtpl = "<tr><td>${id}</td><td>${name}</td><td>${email}</td></tr>";
$.template( "readTemplate", readtpl );
 $(document).ready(function(){
    $( '#tabs' ).tabs({
            fx: { height: 'toggle', opacity: 'toggle' }
        });
        $.ajax({
            url: 'index.php/user/read',
            datatype: 'json',
            success: function(response){
                $.tmpl( "readTemplate", response ).appendTo( "#records" );
            }
        });
    });