由于我一直使用dataTable来处理来自数据库的数据,因此我被AJAX错误卡住了。
这是我的代码在视图,
<table id="prodTable" class="ui red very basic collapsing celled table tableProd" cellspacing="0" width="113%">
<thead>
<tr>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
@if(isset($wholesalerConfig) && !empty($wholesalerConfig))
<?php $products = DB::connection($wholesalerdb)->table('product')->get(); ?>
@foreach($products as $product)
<tr class="griDdProd sep" id="productData">
<td class="colProd">
<div class="field">
<img class="imglist" src="data:image;base64,'..'"">
<h2>{{$product->Name}}</h2>
</div>
<p> </p>
<div class="field">
{{$product->Description}}
</div>
<p> </p>
<div class="field">
<h3>{{$product->Price}}</h3>
</div>
<p> </p>
<div class="field">
<i class="large add to cart icon">
<img src="{{URL::asset('images/add.png')}}" height="23px" width="80px" >
</i>
</div>
</td>
<td>
{{$product->Category}}
{{$product->Subcategory}}
{{$product->Field1}}
{{$product->Field2}}
{{$product->Field3}}
</td>
</tr>
@endforeach
@endif
</tbody>
</table>
上面的表格读取了我所有的数据,并在这里显示。而在js这边…我得到了这个
$(document).ready(function() {
$('#prodTable').DataTable( {
"processing": true,
"serverSide": true,
"pagingType": "full_numbers",
"columnDefs": [
{
"targets": [ 1 ],
"visible": false,
},
],
"ajax": {
"url": "postDatatable",
"type": "POST",
"headers": {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
},
"columns": [
{ "data": "Name" },
{ "data": "Description" },
]
} );
} );
此外,在控制器端,
public function searchData()
{
// $whosalerDb = Session::get('wholesalerDb');
// if(isset($whosalerDb) && !empty($whosalerDb))
// {
// DB table to use
$table = DB::table('wholesaler1');
// Table's primary key
$primaryKey = 'ID';
// Array of database columns which should be read and sent back to DataTables.
// The `db` parameter represents the column name in the database, while the `dt`
// parameter represents the DataTables column identifier. In this case object
// parameter names
$columns = array(
array( 'db' => 'Name', 'dt' => 'Name' ),
array( 'db' => 'Description', 'dt' => 'Description' ),
);
// SQL server connection information
$sql_details = array(
'user' => 'root',
'pass' => '',
'db' => 'wholesaler1',
'host' => 'localhost'
);
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* If you just want to use the basic configuration for DataTables with PHP
* server-side, there is no need to edit below this line.
*/
include(app_path().''classes'ssp.class.php' );
echo json_encode(
SSP::simple( $_POST, $sql_details, $table, $primaryKey, $columns )
);
}
}
显示错误如下1)服务器端方法异常错误,以及视图端Ajax错误
my route is
Route::post('postDatatable','PostController@searchData');
代替
echo json_encode(
SSP::simple( $_POST, $sql_details, $table, $primaryKey, $columns )
);
:
return response()->json(SSP::simple($_POST, $sql_details, $table, $primaryKey, $columns));