我使用AJAX POST请求来填充我的数据表。在我的php文件中,我有一个查询并将其存储在数组上并以json形式返回。但是,即使php和数据库中的字段相同,它也会返回“此请求无可用响应数据”。如果我包含UserName,则会发生错误。请帮我解决这个问题。
我已经将我的数据库字段名称从UserName更改为username,但仍然无法使用。
PHP文件。 (user_action.php)
if ($_POST["action"] == 'fetch') {
$query = "SELECT * FROM userslist ";
if (isset($_POST["search"]["value"])) {
$query .= 'WHERE UserName LIKE "%' . $_POST["search"]["value"] . '%" ';
}
if (isset($_POST["order"])) {
$query .= 'ORDER BY ' . $_POST['order']['0']['column'] . ' ' . $_POST['order']['0']['dir'] . ' ';
} else {
$query .= 'ORDER BY id DESC ';
}
if ($_POST["length"] != -1) {
$query .= 'LIMIT ' . $_POST['start'] . ', ' . $_POST['length'];
}
$statement = $connect->prepare($query);
$statement->execute();
$result = $statement->fetchAll();
// var_dump($result);
// exit();
$data = array();
$filtered_rows = $statement->rowCount();
foreach ($result as $row) {
// var_dump($row["window"]);
// exit();
$sub_array = array();
$sub_array[] = $row["id"];
$sub_array[] = $row['UserName'];
$sub_array[] = $row["UserDesignation"];
$sub_array[] = $row["UserType"];
$sub_array[] = $row["UserDesc"];
$sub_array[] = $row["status"];
$sub_array[] = $row["window"];
$sub_array[] = '<button type="button" name="edit_user" class="btn btn-primary btn-sm edit_user" id="' . $row["id"] . '"><i class="fa fa-pencil"></i> Edit</button> <button type="button" name="delete_user" class="btn btn-danger btn-sm delete_user" id="' . $row["id"] . '"><i class="fa fa-trash"></i> Delete</button>';
$data[] = $sub_array;
}
$output = array(
"draw" => intval($_POST["draw"]),
"recordsTotal" => $filtered_rows,
"recordsFiltered" => get_total_records($connect, 'userslist'),
"data" => $data
);
echo json_encode($output);
}
AJAX(user.php)
var dataTable = $('#user_table').DataTable({
"processing": true,
"serverSide": true,
"order": [],
"ajax": {
url: "dal/user_action.php",
type: "POST",
data: {
action: 'fetch'
}
// dataType: "json",
// error: function(result) {
// alert(result);
// }0
},
"columnDefs": [{
"targets": [0, 1, 2, 3, 4, 5, 6],
// "targets": [0, 1, 2, 3, 4, 5, 6, 7],
"orderable": false,
}, ],
});
错误:
DataTables警告:表id = user_table-无效的JSON响应。有关此错误的更多信息,请参见http://datatables.net/tn/1
和
此请求无响应数据
答案 0 :(得分:0)
在这种情况下,您的查询似乎格式错误,因为您尚未设置const DatePickerField = ({ name, value, onChange }) => {
return (
<DatePicker
selected={(value && new Date(value)) || null}
onChange={val => {
onChange(name, val);
}}
/>
);
};
const App = () => (
<Formik
initialValues={{ date: "" }}
...
>
{props => {
const {
values,
handleSubmit,
setFieldValue
...
} = props;
return (
<form onSubmit={handleSubmit}>
<DatePickerField
name="date"
value={values.date}
onChange={setFieldValue}
/>
...
的值,并且以下比较结果为true
$_POST['length']
尝试先设置默认值
if ($_POST["length"] != -1){ ... }