数据表服务器响应未显示

时间:2014-02-09 12:19:39

标签: php jquery ajax json datatables

我遇到了有关datables服务器端处理的服务器响应的问题。到目前为止,ajax调用正在运行:

$('#result_tbl').dataTable( {
    "bProcessing": true,
    "bServerSide": true,
    "sAjaxSource": "search_synagogue_ajax.php",
    "aoColumns":[ 
                  { "sTitle": "Column1", "mData": "string" },
                  { "sTitle": "Column2", "mData": "stirng" }                    
                ],
    "fnServerData": function ( sSource, aaData, fnCallback ) {

        var countries = new Array();

        aaData.push({"name": "country", "value": countries});

        console.log(aaData);

        $.ajax( {
            "dataType": 'json',
            "type": "POST", 
            "url": sSource, 
            "data": aaData, 
            "success": function(data) {
                 console.log(data);
            },
            error: function(xhr, status, error) {
                 var err = eval("(" + xhr.responseText + ")");
                 alert(err.Message);
            }
         });  
);

我的回复创建如下:

$array = array(
    "aaData" => array(
        array(
            'test1',
            'test1',
        ),
        array(
            'test2',
            'test2',
        )
    )
);

echo json_encode($array);

我得到了:

{"aaData":[["test1","test1"],["test2","test2"]]}

但是,该表不会显示给定的testdata。 Ajax的成功被称为...我注意到有些人声称数据表中不允许使用大括号< 1.8(我正在使用1.9.4)。有谁知道我在这里做错了什么?

编辑:也许我理解错了。我想要做的是将一些参数发布到ajax文件,然后获取它的响应并将其插入到我的数据表中?我是否必须在ajax成功回调中手动执行此操作?

EDIT2:还是不能让它运转起来?有什么建议吗?

EDIT3:尝试了一个更简单的例子,即使没有成功:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">

    <!-- Always force latest IE rendering engine (even in intranet) & Chrome Frame
    Remove this if you use the .htaccess -->
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">

    <title>clientAjax</title>
    <meta name="description" content="">
    <meta name="author" content="">

    <script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
    <script type="text/javascript" src="lib/DataTables-1.9.4/media/js/jquery.dataTables.min.js"></script>

    <script type="text/javascript">
      $(document).ready(function() {
        $('.hist_data').on('click', function(){
            $('.myTable').dataTable( {
                "bProcessing": false,
                "bServerSide": true,
                "bJQueryUI": true,
                "sAjaxSource": "serverAjax.php",
                "sAjaxDataProp": "data",
                "fnServerData" : function(sSource, aoData, fnCallback) {
                    request = $.ajax({
                        "dataType" : "json",
                        "type" : "POST",
                        "url" : sSource,
                        "data" : aoData,
                        "success" : function(data) {
                          console.log(data);
                        },
                        "error": function(xhr, status, error) {
                          var err = eval("(" + xhr.responseText + ")");
                          alert(err.Message);
                        }
                    });
                },
                "aoColumns": [
                    { "mData": "id" },
                    { "mData": "name" },]
            } );

        });
      });
    </script>
  </head>

  <body>
    <div>
      <header>
        <h1>clientAjax</h1>
      </header>
      <nav>
        <p>
          <a href="/">Home</a>
        </p>
        <p>
          <a href="/contact">Contact</a>
        </p>
      </nav>

      <div class="hist_data">Click here</div>
      <table class="myTable">

      </table>

      <footer>
      </footer>
    </div>
  </body>
</html>

和Ajax文件:

<?php

$array = array(
    "aaData" => array(
        array(
            'id' => 1,
            'name' => "ONE"
        ),
        array(
            'id' => 2,
            'name' => "TWO"
        )
    )
);

echo json_encode($array);

?>

2 个答案:

答案 0 :(得分:0)

尝试从上一个数组项中删除逗号,如下所示

$array = array(
"aaData" => array(
    array(
        'test1',
        'test1'
    ),
    array(
        'test2',
        'test2'
    )
)

);

echo json_encode($ array);

答案 1 :(得分:0)

我自己想通了!

我需要做的是添加:

fnCallback(data);

成功回调,数据出现在表格中!