如何从php获取json数据的数据库jquery插件

问题描述:

我对PHP很新,我试图使用Datatables jQuery插件。我明白我必须从.php文件中获取数据,然后在我的数据表中使用它,但我似乎不能通过ajax将其传递给数据。我设法从数据库中获取数据,并对其进行编码,但是我不知道如何将它调用到我的index.php文件中,我需要显示它。我究竟做错了什么?
这是我的代码:

I am very new to PHP and I am trying to use the Datatables jQuery plugin. I understand I have to fetch my data from a .php file, and then use it in my datatable, but I can't seem to pass it to datatables via ajax. I manage to fetch my data from the database, and json encode it, but then I do not know how to call it into my index.php file, where I need to display it. What am I doing wrong? Here is my code:

HTML(编辑):

       <!-- DataTables CSS -->
< link rel='stylesheet' type='text/css' href='http://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.4/css/jquery.dataTables.css'>

< !-- jQuery -->
< script type='text/javascript' charset='utf8' src='http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.2.min.js'></script>

<!-- DataTables -->
< script type='text/javascript' charset='utf8' src='http://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.4/jquery.dataTables.min.js'></script>

< script type='text/javascript'>
  $( document ).ready(function() {
$('#tabela').dataTable({
                  'bJQueryUI' : true,
                  'sPaginationType' : 'full_numbers',
                  'bRetrieve' : true,
                  'bFilter' : true,
                  'iDisplayLength': 10,
                  'bProcessing' : true,
                  "sAjaxSource": "adminmysqli.php"

        });   
});
< /script>

来自fetchdata.php的数据:

data from fetchdata.php:

{"iTotalRecords":"41","iTotalDisplayRecords":"41","aaData":
[["2","Arya","Stark","612345555","Braavos"],
["3","Jon","Snow","612345655","The"], 
["4","Meryn","Trant","612345679","Tirane"],  
["5","Sansa","Stone","692345678","Durres"],
["6","Myda","Slate","612345676","Tirane"],
["7","Varys","Bird","612345689","Tirane"], 
["9","Stannis","Baratheon","612345678","Tirane"],
["10","Shireen","Burn","612345678","Iron"],
["11","Selyse","Tully","612345678","Tirane"],
["12","Lyanna","Dread","612345678","Tirane"],
["13","Viserys","Targaryen","612345678","Durres"],
["14","Daennerys","Stormborn","612345678","Tirane"],
["15","Khaal","Drogo","612345678","Tirane"],
["16","Jojen","Reed","612345678","Tirane"],
["17","Theon","Greyjoy","612345678","Tirane"],
["18","Osha","Green","612345678","Tirane"],
["19","Oberyn","Martell","612345678","Tirane"],
["20","Ellaria","Sands","612345678","Tirane"],
["22","Tommen","Laster","612345678","Tirane"],
["23","Robert","Baratheon","612345678","Tirane"],
["24","Jamie","Lannister","612345678","Tirane"],
["25","Tywin","Bolt","612345678","Tirane"],
["26","Tyrion","Imp","612345678","Tirane"],
["27","Gregor","Clegane","612345678","Tirane"],
["28","Qyburn","Exper","612345678","Tirane"],
["30","Daniel","Howell","612345678","Tirane"],
["31","Samwell","Tarly","612345678","Tirane"],
["32","Aemon","Maester","61234569","Tirane"],
["33","Jaqen","Hgar","612345678","Tirane"],
["34","Daario","Naharis","612345678","Tirane"],
["35","Jorah","Bear","612345678","Tirane"],
["36","Irri","Mereen","612345678","Tirane"],
["37","Margaery","Tyrell","612345678","Tirane"],
["38","Renly","Baratheon","612345678","Tirane"],
["44","Eddard","Stark","697845123","Winterfell"],
["45","Miranda","Hart","692314256","Durres"],
["46","Stray","Cat","691234567","Here"],
["48","Haley","Dunphy","653746111","Tirana"],
["51","Asli","Felin","666111222","South"],
["55","Alison","Brie","667755333","Saye"],
["58","Marie","Curie","665544333","Radium"]]}

编辑:
感谢您的回复,我设法获取格式化的数据,我删除了mData属性,我也错误地加载了一个脚本,阻止我获取数据。

Thank you for your replies, I managed to get the formatted data my removing the mData attributes, also I had wrongly loaded a script, preventing me to get the data.

将您的sAjaxSource路径更改为您的php文件,然后重试使用以下代码:

Change your sAjaxSource path to your php file and try again using these code:

<script type='text/javascript'>
$( document ).ready(function() {
    $('#tabela').dataTable({
        'bJQueryUI' : true,
        'sPaginationType' : 'full_numbers',
        'bRetrieve' : true,
        'bFilter' : true,
        'iDisplayLength': 10,
        'bProcessing' : true,
        "sAjaxSource": "fetchdata.php"
    });
});
</script>