为dataTable jquery插件获取数组时出现“非法字符串偏移”错误

我创建了一个ajax json响应,该响应将显示在我的dataTable jQuery插件中。该表的ID为#dataTable。

这是dataTable插件的代码:

$(document).ready(function() {
    var productTable = $("#dataTable").DataTable({
        "ajax": "../api/ajax/getProduct.php","order": [[ 1,"desc" ]]
    });
});

这是getProduct.php

<?php
include_once('../../components/db.php');

$sqlb = "SELECT * FROM products WHERE status='active'";
$resultb = $conn->query($sqlb);
$data = $resultb->fetch_assoc();

$result = array();

foreach ($data as $key => $value) {
    $image = '<img width="50" height="40" class="rounded-circle" src="../' . $value['image'] . '; ?>">';

    $buttons = '<a href="product-update.php' . $value["id"] . '" class="btn btn-info btn-sm"><i class="fa fa-edit" aria-hidden="true"></i></a><a onclick="removeProduct(' . $value["id"] . ')" class="btn btn-danger btn-sm"><i class="fa fa-trash" aria-hidden="true"></i></a>';

    $result[$key] = array(
        $value["description"],$value["price"],$image,$value["availability"],$buttons,);
}// /foreach

echo json_encode($result);
?>

这是我检查XHR时遇到的错误

Warning: Illegal string offset 'image' in C:\xampp\htdocs\copy\api\ajax\getProduct.php on line 11

Warning: Illegal string offset 'id' in C:\xampp\htdocs\copy\api\ajax\getProduct.php on line 13

Warning: Illegal string offset 'id' in C:\xampp\htdocs\copy\api\ajax\getProduct.php on line 13

Warning: Illegal string offset 'description' in C:\xampp\htdocs\copy\api\ajax\getProduct.php on line 16

Warning: Illegal string offset 'price' in C:\xampp\htdocs\copy\api\ajax\getProduct.php on line 17

Warning: Illegal string offset 'availability' in C:\xampp\htdocs\copy\api\ajax\getProduct.php on line 19

这是我在页面加载时从dataTable本身得到的弹出错误。

DataTables warning: table id=dataTable - Invalid JSON response. For more information about this error,please see http://datatables.net/tn/1

似乎真正的问题是编码的数据本身是错误的。遇到此错误,我无法将数据显示到dataTable本身中。

huanghongwang123 回答:为dataTable jquery插件获取数组时出现“非法字符串偏移”错误

基于var_dump($data);结果:

 array(9) {
  ["id"]=>
  string(1) "2"
  ["names"]=>
  string(12) "Fruity Split"
  ["price"]=>
  string(5) "50.00"
  ["qty"]=>
  string(1) "1"
  ["image"]=>
  string(26) "images/products/menu-2.jpg"
  ["description"]=>
  string(90) "Dessert made with a split banana,ice cream,sauce,whipped cream,nuts,and a strawberry."
  ["category"]=>
  string(7) "dessert"
  ["availability"]=>
  string(9) "Available"
  ["status"]=>
  string(6) "active"
}

它给您这个错误的原因:

  

警告:字符串偏移量非法

这是因为您仅循环 1行

改为将代码更改为此:

<?php
include_once('../../components/db.php');

$sqlb = "SELECT * FROM products WHERE status='active'";
$resultb = $conn->query($sqlb);

// This just return single row
// $data = $resultb->fetch_assoc();

$result = array();

//Use while instead of foreach
while ($value =  $resultb->fetch_assoc()) {
    $image = '<img width="50" height="40" class="rounded-circle" src="../' . $value['image'] . '; ?>">';

    $buttons = '<a href="product-update.php' . $value["id"] . '" class="btn btn-info btn-sm"><i class="fa fa-edit" aria-hidden="true"></i></a><a onclick="removeProduct(' . $value["id"] . ')" class="btn btn-danger btn-sm"><i class="fa fa-trash" aria-hidden="true"></i></a>';

    // Add Keys For DataTable column
    $result[] = array(
        'description' => $value["description"],'price' => $value["price"],'image' => $image,'availability' => $value["availability"],'buttons' => $buttons,);
}

echo json_encode($result);
?>
,

根据@Bluetree的答案,不需要为dataTable添加键。

正确的方法是,首先应在数据编码后的数据数组中输入数据字符串“ data”,因为我们只需要遵循dataTable所需的格式即可。

<?php
include_once('../../components/db.php');

$sql = "SELECT * FROM products WHERE status='active'";
$query = $conn->query($sql);

while($data=$query->fetch_assoc()){
    $image = '<img width="50" height="40" class="rounded-circle" src="../' . $data['image'] . '">';

    $buttons = '<a href="product-update.php' . $data['id'] . '" class="btn btn-info btn-sm"><i class="fa fa-edit" aria-hidden="true"></i></a><a onclick="removeProduct(' . $data['id'] . ')" class="btn btn-danger btn-sm"><i class="fa fa-trash" aria-hidden="true"></i></a>';

    // instead of just $result[],we need to use $result[data][] in order to use it for dataTable
    $result["data"][] = array(
        $data['names'],$data['description'],$data['price'],$image,$data['availability'],$buttons,);
}

echo json_encode($result);

// console.log output would be {"data":[["Blueberry Cheesecake"," This blueberry cheesecake starts with a buttery graham cracker crust,a creamy cheesecake center,and a tangy blueberry swirl.","80.00","","Available","<\/i><\/a><\/i><\/a>"],["Fruity Split","Dessert made with a split banana,and a strawberry.","50.00",["Pancake","Pancake topped with blueberry and strawberry.",["Steak","Steak. . . . . in which I need to output
?>

dataTable中的AJAX需要具有URL和数据本身。 只需提供:

$('#example').dataTable( {
  "ajax": "data.json",}
} );

将是这样编码的简写方式:

$('#example').dataTable( {
  "ajax": {
    "url": "data.json","data": {
        "user_id": 451
    }
  }
} );

因为ajax的第一个示例格式已经具有url和数据本身。 url中的返回值是数据本身,而ajax的URL是URL本身。因此,我们需要在数组的编码结果内的返回值中包含一个“数据”。

本文链接:https://www.f2er.com/3147323.html

大家都在问