【问题标题】:How to show data from controller in ajax function success如何在ajax功能成功中显示来自控制器的数据
【发布时间】:2017-03-23 20:44:00
【问题描述】:

我尝试使用 ajax 函数在控制器的 html 页面中显示数据,当我尝试在函数成功时提醒数据但它显示 json 数据和当前页面 html 标记。我无法在表格视图中显示数据。请帮我。 下面显示代码:

$(".statuss").on("click",'', function(){
  var ele=$(this).closest('tr');
    var id=document.querySelector('.statuss:checked').value;
   var site=document.getElementById('sites').value;
   //alert(id);
  // alert(site);
  var isGood = confirm('Are you Sure??'); 
  if (isGood) {
      $.ajax({
            type: 'post',
            url: "<?php echo site_url("dashboard/get_keyword");?>",
            data: {
              id:id,
              site:site,
            },
           // dataType: 'json',
            success: function(data) {
                //console.log(data);
            $("#table_result").removeClass("hidden");
            $("#table_result_list").removeClass("hidden");
            $("#table_result_footer").removeClass("hidden");
            $("#table_inputs_head").removeClass("hidden");

            alert(data);
            //for {
                var d = $.parseJSON(data);
            //}
            alert(d);    
                var output;
                $.each(d,function(i,e) {
          // here you structured the code depend on the table of yours
           output += '<tr><td>'+e.keyword+'</td><td>'+e.id+'</td></tr>';
          // alert(e.keyword);
            });

          //  alert(datas);
            $("#table_result_list").html(data);
          //   $("#table_result_list").append(output);

            // alert(data);

            }
        });
    } 
  });        

【问题讨论】:

    标签: ajax codeigniter


    【解决方案1】:

    像这样更改控制器中的标题..

    header('Content-Type: application/x-json; charset=utf-8');
    $result= // your code.......
    echo json_encode($result);
    

    【讨论】:

    • 感谢您的回复
    【解决方案2】:

    在你的控制器中打印控制器本身的表格或创建一个视图文件来打印表格,

    在你的 ajax 函数中

    success: function(data) {
    
     $("#table_result_list").html(data);
    }
    

    【讨论】:

    • 现在它可以工作了,我在控制器中编写了表格 html 代码,并在函数成功时将此数据附加到特定的 div id
    • 感谢您的回复
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-08-23
    • 1970-01-01
    • 2020-09-02
    • 2014-10-10
    • 2023-03-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多