我不明白我做错了什么。这不是执行ajax调用和返回JSON的正确格式吗?
我正在尝试返回JSON来填充DataTable。我以前做过类似的事情,但这次我不能让它工作。
下面是我使用SQLSRV返回JSON的脚本("api/exceptions_all.php"):
 <?php
   include("../include/database.php");
   $select = "SELECT
               ''
               ,[FOR_PARTNER] 
               ,[FOR_NAME]
               ,[SHP_PARTNER]
               ,[SHP_NAME]
               ,[MODDATE]
               ,[MODUSER]
               ,[ID]
             FROM [main].[dbo].[for_exceptions]";
 $query = sqlsrv_query($dbc, $select) or die(sqlsrv_errors());
 $out = array();
 while( $row = sqlsrv_fetch_array($query, SQLSRV_FETCH_ASSOC) ) 
 {
   $out[] = $row;
 }
 echo json_encode( $out );
 sqlsrv_free_stmt($query);
 ?>下面是我的javascript文件("exceptions.js"),我试图在其中检索JSON并将其打印到数据表中:
 $(document).ready(function()
 {
   var $dataTable = $('#example1').DataTable({
     "type": 'POST',
     "ajax": 'api/exceptions_all.php',
     "data": data,
     "dataType": 'json',
     "bDestroy": true
   });
 });我一直收到一个错误,上面提到“ReferenceError”:data时,会说“未捕获数据:数据未定义”。
在我的HTML文件中,我有一个应该填充DataTable的表:
 <table id="example1">
 <thead>
   <tr>
     <th>Edit</th>
     <th>FF Partner Code</th>
     <th>FF Name</th>
     <th>SHP Partner Code</th>
     <th>SHP Name</th>  
     <th>Modified Date</th>
     <th>Modified User</th>
     <th>ID</th>
   </tr>
 </thead>
 // datatable should be here
 </table>发布于 2016-07-21 02:32:42
看起来您正在使用jQuery的DataTables插件...
像这样的东西也许..。
$(document).ready(function()
{
   var $dataTable = $('#example1').DataTable({
     "ajax": 'api/exceptions_all.php'
   });
});这是直接从文档中得到的。
https://stackoverflow.com/questions/38487551
复制相似问题