Javascript 使用服务器端数据表返回空ajax
我面临服务器端脚本的问题。行数正确,但数据为空。还有一个错误,即值不能为空或NULL 我已经向脚本中的列添加了defaultContent,其中包含消息Javascript 使用服务器端数据表返回空ajax,javascript,ajax,datatables,Javascript,Ajax,Datatables,我面临服务器端脚本的问题。行数正确,但数据为空。还有一个错误,即值不能为空或NULL 我已经向脚本中的列添加了defaultContent,其中包含消息Not set,现在行是正确的,但我只有Not set通知。 我试图添加UTF-8字符集,但仍然不起作用 JS用于服务器端: var DatatablesDataSourceAjaxServer= { init:function() { $("#m_table_1").DataTable( { responsive:tru
Not set
,现在行是正确的,但我只有Not set
通知。
我试图添加UTF-8字符集,但仍然不起作用
JS用于服务器端:
var DatatablesDataSourceAjaxServer= {
init:function() {
$("#m_table_1").DataTable( {
responsive:true,
searchDelay:500,
processing:true,
serverSide:true,
ajax:"assets/data/scripts/data-user-table.php",
columns:[ {
data: "name",
//defaultContent: "<i>Not set</i>"
}
, {
data: "mail",
defaultContent: "<i>Not set</i>"
}
, {
data: "status",
defaultContent: "<i>Not set</i>"
}
, {
data: "role",
defaultContent: "<i>Not set</i>"
}
, {
data: "created",
defaultContent: "<i>Not set</i>"
}
, {
data: "login",
defaultContent: "<i>Not set</i>"
}
, {
data: "Actions"
}
], columnDefs:[ {
targets:-1,
title:"Actions", orderable:!1, render:function(a, e, t, n) {
return'\n <a href="" class="m-portlet__nav-link btn m-btn m-btn--hover-brand m-btn--icon m-btn--icon-only m-btn--pill" title="View or edit account">\n <i class="la la-edit"></i>\n </a> <a href="#" class="m-portlet__nav-link btn m-btn m-btn--hover-brand m-btn--icon m-btn--icon-only m-btn--pill" title="Cancel account">\n <i class="la la-trash"></i>\n </a>'
}
}
, {
targets:2, render:function(a, e, t, n) {
var s= {
1: {
title: "Pending", class: "m-badge--brand"
}
, 2: {
title: "Delivered", class: " m-badge--metal"
}
, 3: {
title: "Canceled", class: " m-badge--primary"
}
, 4: {
title: "Success", class: " m-badge--success"
}
, 5: {
title: "Info", class: " m-badge--info"
}
, 6: {
title: "Danger", class: " m-badge--danger"
}
, 7: {
title: "Warning", class: " m-badge--warning"
}
}
;
return void 0===s[a]?a:'<span class="m-badge '+s[a].class+' m-badge--wide">'+s[a].title+"</span>"
}
}
, {
targets:3, render:function(a, e, t, n) {
var s= {
1: {
title: "Online", state: "danger"
}
, 2: {
title: "Retail", state: "primary"
}
, 3: {
title: "Direct", state: "accent"
}
}
;
return void 0===s[a]?a:'<span class="m-badge m-badge--'+s[a].state+' m-badge--dot"></span> <span class="m--font-bold m--font-'+s[a].state+'">'+s[a].title+"</span>"
}
}
]
}
)
}
}
;
jQuery(document).ready(function() {
DatatablesDataSourceAjaxServer.init()
}
);
var datatabledatasourceajaxserver={
init:function(){
$(“#m#u table_1”)。数据表({
回答:是的,
搜索延迟:500,
处理:对,
服务器端:是的,
ajax:“资产/数据/脚本/数据用户表.php”,
列:[{
数据:“名称”,
//defaultContent:“未设置”
}
, {
数据:“邮件”,
defaultContent:“未设置”
}
, {
数据:“状态”,
defaultContent:“未设置”
}
, {
数据:“角色”,
defaultContent:“未设置”
}
, {
数据:“已创建”,
defaultContent:“未设置”
}
, {
数据:“登录”,
defaultContent:“未设置”
}
, {
数据:“行动”
}
],columnDefs:[{
目标:-1,
标题:“操作”,可排序:!1,呈现:函数(a,e,t,n){
返回'\n'
}
}
, {
目标:2,渲染:函数(a,e,t,n){
变量s={
1: {
标题:“待定”,类别:“m-徽章——品牌”
}
, 2: {
标题:“交付”,类别:“m-徽章——金属”
}
, 3: {
标题:“取消”,课程:“m-badge——初级”
}
, 4: {
标题:“成功”,课程:“m-徽章——成功”
}
, 5: {
标题:“信息”,课程:“m-徽章——信息”
}
, 6: {
标题:“危险”,课程:“m-徽章——危险”
}
, 7: {
标题:“警告”,课程:“m-badge--警告”
}
}
;
返回void 0==s[a]?a:“”+s[a]。title+“”
}
}
, {
目标:3,渲染:函数(a,e,t,n){
变量s={
1: {
标题:“在线”,状态:“危险”
}
, 2: {
标题:“零售”,州:“初级”
}
, 3: {
标题:“直接”,说明:“口音”
}
}
;
返回void 0==s[a]?a:“”+s[a]。title+“”
}
}
]
}
)
}
}
;
jQuery(文档).ready(函数(){
DatatablesDataSourceAjaxServer.init()
}
);
以及data-user-table.php:
<?php
// DB table to use
$table = 'users';
// Table's primary key
$primaryKey = 'uid';
// 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 simple
// indexes
$columns = array(
array( 'db' => 'name', 'dt' => 0 ),
array( 'db' => 'mail', 'dt' => 1 ),
array( 'db' => 'status', 'dt' => 2 ),
array( 'db' => 'role', 'dt' => 3 ),
array(
'db' => 'created',
'dt' => 4,
'formatter' => function( $d, $row ) {
return date( 'jS M y', strtotime($d));
}
),
array(
'db' => 'login',
'dt' => 5,
'formatter' => function( $d, $row ) {
return '$'.number_format($d);
}
)
);
// SQL server connection information
$sql_details = array(
'user' => '',
'pass' => '',
'db' => '',
'host' => '',
'charset' => 'utf8'
);
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* If you just want to use the basic configuration for DataTables with PHP
* server-side, there is no need to edit below this line.
*/
require( 'ssp.class.php' );
echo json_encode(
SSP::simple( $_GET, $sql_details, $table, $primaryKey, $columns )
);
我不熟悉在php中使用ajax,但是您不需要“success:…”部分来返回调用,以及将代码放在哪里,以便在返回时做一些事情(包括读取数据)?抱歉,如果是这样的话,这可能会偏离正轨。是的,通常是这样,但在我在DataTables上发现的示例中,没有必要这样做