简体   繁体   English

Codeigniter:AJAX调用后返回JSON数据,但无法成功运行:function()

[英]Codeigniter: JSON data is returned after AJAX call but it cannot run the success: function()

My Ajax 我的阿贾克斯

 // Load category function function load_category() { $.ajax({ url: "<?php echo base_url() ?>index.php/admin/departments", type: "GET", dataType: 'json', success: function(data){ $('#category-row').html(data); }, error: function(){ alert('Error....'); } }); } // Fix tooltip not work $('body').tooltip({ selector: '[data-toggle="tooltip"]' }); }); 

My table 我的桌子

This where I want to echo that data... 我要在这里回显该数据...

 <div class="row"> <div class="col-sm-12"> <div class="card-box table-responsive"> <table id="datatable-responsive" class="table table-striped table-bordered dt-responsive nowrap" cellspacing="0" width="100%"> <thead> <tr> <th>Dep't Name</th> <th>Description</th> <th>Consultancy Amount</th> <th>Doctor's Amount</th> <th>Manage Department</th> </tr> </thead> <tbody id="category-row"> </tbody> </table> </div> </div><!-- end col --> </div> <!-- end row --> 

My Contoller now 现在我的Contoller

admin/departments 管理员/部门

 public function departments()
{

    $table ='';

    $departments = $this->admin_model->departments();

    if(count($departments)>0)
    {
      foreach ($departments as $dept)
      {
        $table.='<tr id="row-id-'.$dept->Dept_id.'">';
        $table.='<td>'.$dept->Dept_name.'</td>';
        $table.='<td>'.$dept->Description.'</td>';
        $table.='<td>'.$dept->Consultancy_amount.'</td>';
        $table.='<td>'.$dept->Doctor_Percentage.'</td>';
        $table.='<td>';
        $table.='<a href="#" data-toggle="modal" data-target="#myModalUpdate"><i class="glyphicon glyphicon-edit" data-toggle="tooltip" title="EDIT DEPARTMENT"></i></a> |';
        $table.='<a href="#" data-toggle="modal" data-target="#myModalDelete"><i class="glyphicon glyphicon-trash" data-toggle="tooltip" title="DELETE DEPARTMENT"></i></a>';
    $table.='</td>';
    $table.='</tr>';
      }
      echo json_encode($table);     
   }
}

What exactly happens is that the window opens with the load_category() function. 实际发生的是使用load_category()函数打开窗口。 However, when I access it, it comes as JSON just as below 但是,当我访问它时,它作为JSON如下所示

"ENT<\/td>Ear, Nose and Throat<\/td>30000<\/td>40<\/td><\/i><\/a> |<\/i><\/a><\/td><\/tr>Cancer<\/td>Cancer<\/td>100000<\/td>50<\/td><\/i><\/a> |<\/i><\/a><\/td><\/tr>ENT<\/td>Ear<\/td>40000<\/td>34<\/td><\/i><\/a> |<\/i><\/a><\/td><\/tr>"

I am totally confused about why it cant run the success funtion and echo my data into where I want it. 我完全困惑为什么它无法运行成功功能并将数据回显到我想要的位置。

Thank you very much 非常感谢你

I'm not sure you want to do an .html() output on a table body ( $('#category-row').html(data); ), so that might be where your problem lies. 我不确定您是否要在表主体( $('#category-row').html(data); )上执行.html()输出,因此这可能就是问题所在。 If you are wanting to add rows, I think you need to do an append. 如果您想添加行,我认为您需要进行追加。

$("#category-row").append(data);

Also, I might suggest a different method of parsing the data. 另外,我可能会建议使用另一种解析数据的方法。 I would return the whole $departments result via the json_encode, and then parse through that in the success function like my example below. 我将通过json_encode返回整个$departments结果,然后在成功函数中进行解析,例如下面的示例。 In that example, I am also changing your controller's usage of count and going with if not empty ( !empty($departments) ). 在该示例中,我还更改了控制器的count用法, 如果不为空继续使用( !empty($departments) )。

Controller function: 控制器功能:

public function departments()
{
    $departments = $this->admin_model->departments();

    if(!empty($departments)) {
        echo json_encode($departments);     
    }
}

jQuery 'success' adjustment: jQuery“成功”调整:

success: function(data){
    var tableRows = '';

    $.each(data, function(index, dept) {

        tableRows = tableRows + '<tr id="row-id-' + dept.Dept_id + '"><td>' + dept.Dept_name + '</td><td>' + dept.Description + '</td><td>' + dept.Consultancy_amount + '</td><td>' + dept.Doctor_Percentage + '</td><td><a href="#" data-toggle="modal" data-target="#myModalUpdate"><i class="glyphicon glyphicon-edit" data-toggle="tooltip" title="EDIT DEPARTMENT"></i></a> | <a href="#" data-toggle="modal" data-target="#myModalDelete"><i class="glyphicon glyphicon-trash" data-toggle="tooltip" title="DELETE DEPARTMENT"></i></a></td>/tr>';

    });

    $("#category-row").append(tableRows);
}

More food for thought...if you want to test that the success is firing at all, put a simple alert('success'); 更多的思考...如果您想测试成功的根源,请发出一个简单的警报(“成功”); or console.log('success') in there and see if it fires off. 或console.log('success')那里,看看它是否启动。 Maybe your query is empty. 也许您的查询为空。

Anyway, hope this helps. 无论如何,希望这会有所帮助。

The whole problem was that I was loading JQuery at the bottom not at the top. 整个问题是我在底部而不是顶部加载了JQuery。 This handicapped me as I could access it. 这使我无法使用它。

I concluded that if ypu load jquery right at the bottom, Please use this: 我得出的结论是,如果ypu在底部直接加载jquery,请使用以下命令:

 (function($) {

//All your functions go here. Be it Ajax!

        }(jQuery));

Thanks 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM