dousi6192 2019-07-20 05:45
浏览 83
已采纳

我在ajax成功中收到我的数据如何在表中显示jquery数据

I fetch data from controller and it shows in console.log(data) and I want to show that data in table

View:

<?php include 'header.php';?>
<script src="/assets/js/plugins/jquery.min.js"></script>
<script src="/assets/js/jquery.validate.min.js"></script>
<script>
 var jq = $.noConflict();
</script> 
<table id="enquiry_table">
<thead>
 <tr>
    <th>From</th>
    <th>TO</th>
    <th>Date</th>
    <th>Status</th>
 </tr>
</thead>
<tbody>
   <?php 
      foreach ($records as $rec){
    ?>
  <tr>
      <td><?php echo $rec[0]->lr_from; ?></td> 
      <td><?php echo $rec[0]->lr_to; ?></td>               
      <td><?php echo date('d-m-Y',strtotime($rec->date));?></td>
      <td><?php echo $rec->status;?> </td> 
  </tr>
    <?php
    }
  ?>
</tbody>
</table>

<script type="text/javascript">
  function status_form(){
  var lr_no = jq('#lr_no').val();
     jq.ajax({
          url :"https://demo.barque.online/sitecontroller/StatusController/fetchStatus",
          type:"POST",
          dataType: "json",
          data:{
            lr_no:lr_no,
         },
          success: function(data)
          {
            console.log(data);
         },
          error:function(data)
          {
            alert("error message"+data);
          },async:false,
      });    
    }
</script>
<?php include 'footer.php'; ?>

Controller:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class StatusController extends CI_Controller {
  public function __construct()
{
    header('Access-Control-Allow-Origin: my url');
    header('Access-Control-Allow-Credentials: true');
    header('Access-Control-Max-Age: 604800');
    header("Access-Control-Allow-Headers: Origin, Content-Type, Accept, Access-Control-Request-Method");
    header("Access-Control-Allow-Methods: GET, POST");
    parent::__construct();
    $this->load->model("sitemodel/StatusModel",'sModel');
 }
    function fetchStatus(){
    $lr_no                       = $this->input->post('lr_no');
    $statusResult['records']     = $this->sModel->fetchRecords($lr_no);
    echo json_encode($statusResult);
     }
}
  • 写回答

2条回答 默认 最新

  • dqgxazo4483 2019-07-20 07:37
    关注

    try this:-

    <script type="text/javascript">
    
    
     function status_form(){
      var lr_no = jq('#lr_no').val();
         jq.ajax({
              url :"https://demo.barque.online/sitecontroller/StatusController/fetchStatus",
              type:"POST",
              dataType: "json",
              data:{
                lr_no:lr_no,
             },
              success: function(data)
              {
                var html="";
                for(var i=0;i<data.length;i++){
                   html += "<tr>";
                   html += "<td>"+data[i]['your_index']+"</td>";//as per your columns and use this if your data is array. if it was stdObject then use (.)sign for indexing.
                   html += "</tr>";
                }
                $('#userdata').append(html);
                console.log(data);
             },
              error:function(data)
              {
                alert("error message"+data);
              },async:false,
          });    
        }
    
    </script>
    

    and add this:-

    <tbody id="userdata">
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 请问Ubuntu要怎么安装chrome呀?
  • ¥15 视频编码 十六进制问题
  • ¥15 Xsheii7我安装这个文件的时候跳出来另一个文件已锁定文件的无一部分进程无法访问。这个该怎么解决
  • ¥15 unity terrain打包后地形错位,跟建筑不在同一个位置,怎么办
  • ¥15 FileNotFoundError 解决方案
  • ¥15 uniapp实现如下图的图表功能
  • ¥15 u-subsection如何修改相邻两个节点样式
  • ¥30 vs2010开发 WFP(windows filtering platform)
  • ¥15 服务端控制goose报文控制块的发布问题
  • ¥15 学习指导与未来导向啊