我正在尝试为php运行jtable演示,但我总是遇到一个错误: “与服务器通信时发生错误”。
我知道我可以列出json编码,因为我试图打印它,但由于该错误而无法获取表内的数据。
提前致谢
的index.html:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Datatable with mysql</title>
<link href="themes/redmond/jquery-ui-1.8.16.custom.css" rel="stylesheet" type="text/css" />
<link href="Scripts/jtable/themes/lightcolor/blue/jtable.css" rel="stylesheet" type="text/css" />
<script src="scripts/jquery-1.6.4.min.js" type="text/javascript"></script>
<script src="scripts/jquery-ui-1.8.16.custom.min.js" type="text/javascript"></script>
<script src="Scripts/jtable/jquery.jtable.js" type="text/javascript"></script>
<div class="container">
<div class="">
<h1>jTable Demo Server Side</h1>
<div class="col-sm-8" id="employee_grid">
</div>
</div>
</div>
<script type="text/javascript">
$( document ).ready(function() {
$('#employee_grid').jtable({
title: 'List of Employees',
defaultSorting: 'Name ASC',
actions: {
listAction: 'response.php?action=list',
createAction: 'response.php?action=create',
updateAction: 'response.php?action=update',
deleteAction: 'response.php?action=delete'
},
fields: {
personID: {
title: 'EMPId',
width: '10%',
edit: false
},
name: {
title: 'Employee Name',
width: '40%'
},
age: {
title: 'Employee Salary',
width: '20%'
},
recordDate: {
title: 'Age',
width: '30%'
}
}
});
$('#employee_grid').jtable('load');
});
</script>
response.php
<?php
try
{
//Open database connection
$con = mysqli_connect('localhost', 'root', '','aaa') or die("Connection failed: " . mysqli_connect_error());
mysqli_select_db( $con,"aaa");
//Getting records (listAction)
if($_REQUEST["action"] == "list")
{
//Get records from database
$result = mysqli_query($con,"SELECT * FROM `people`;");
//Add all records to an array
$rows = array();
while($row = mysqli_fetch_array($result))
{
$rows[] = $row;
}
//Return result to jTable
$jTableResult = array();
$jTableResult['Result'] = "OK";
$jTableResult['Records'] = $rows;
echo json_encode($jTableResult);
}
else if($_REQUEST["action"] == "create")
{
//Insert record into database
$result = mysqli_query($con, "INSERT INTO people(Name, Age, RecordDate) VALUES('" . $_POST["Name"] . "', " . $_POST["Age"] . ",now());");
//Get last inserted record (to return to jTable)
$result = mysqli_query($con , "SELECT * FROM people WHERE PersonId = LAST_INSERT_ID();");
$row = mysqli_fetch_array($result);
//Return result to jTable
$jTableResult = array();
$jTableResult['Result'] = "OK";
$jTableResult['Record'] = $row;
print json_encode($jTableResult);
}
//Creating a new record (createAction)
//Close database connection
mysqli_close($con);
}
catch(Exception $ex)
{
//Return error message
$jTableResult = array();
$jTableResult['Result'] = "ERROR";
$jTableResult['Message'] = $ex->getMessage();
echo json_encode($jTableResult);
}
?>
那是我不能让这个演示工作。