有没有人知道为什么我的AJAX函数会单独返回每个字符。例如。
testNew.html:150 i
testNew.html:150 t
testNew.html:150 e
testNew.html:150 m
应该在图表上返回完整的单词。但它在控制台上打印单个字母并在图表上打印undefined
。
javascript中的AJAX代码
$(document).ready(function(){
$.ajax({
url: "BaseClass.php",
data: { action : 'getResults' },
datatype: "application/json",
method: "GET",
success: function(data) {
console.log(data); // Produces the whole array of data from the php query fine.
var item = [];
var freq = [];
for(var i in data) {
console.log(data[i]); //produces each letter individually
item.push("Item " + data[i].item_description);
freq.push(data[i].item_id);
}
console.log(data[0].item_description); //Outputs undefined
var chartdata = {
labels: item,
datasets : [
{
label: '...',
backgroundColor: 'rgba(200, 200, 200, 0.75)',
borderColor: 'rgba(200, 200, 200, 0.75)',
hoverBackgroundColor: 'rgba(200, 200, 200, 1)',
hoverBorderColor: 'rgba(200, 200, 200, 1)',
data: freq
}
]
};
var ctx = $("#mycanvas");
var barGraph = new Chart(ctx, {
type: 'bar',
data: chartdata
});
},
error: function(data) {
console.log(data);
}
});
});
MySQLDao.php - 保存所有查询,获取查询结果并发送到javascript:
<?php
error_reporting(E_ALL); ini_set('display_errors', 1);
//Class for holding queries
class MySQLDao
{
var $dbhost = null;
var $dbuser = null;
var $dbpass = null;
var $mysqli = null;
var $dbname = null;
var $result = null;
//constructor
function __construct()
{
$this->dbhost = Conn::$dbhost;
$this->dbuser = Conn::$dbuser;
$this->dbpass = Conn::$dbpass;
$this->dbname = Conn::$dbname;
}
//Attempt a connection to the database
public function openConnection()
{
//Try and connect to the database
$this->mysqli = new mysqli($this->dbhost, $this->dbuser, $this->dbpass, $this->dbname);
//If the connection threw an error, report it
if (mysqli_connect_errno())
{
return false;
}
else
{
return true;
}
}
//Get method for retrieving the database conection
public function getConnection()
{
return $this->mysqli;
}
//Close the connection to the database
public function closeConnection()
{
//If there is a connection to the database then close it
if ($this->mysqli != null)
$this->mysqli->close();
}
//-----------------------------------QUERY METHODS-------------------------------------
public function getResults()
{
$sql = "SELECT * FROM item";
$result = $this->mysqli->query($sql);
//loop through the returned data
$data = array();
foreach ($result as $row) {
$data[] = $row;
}
return $data;
}
}
?>
BaseClass.php:
<?php
error_reporting(E_ALL); ini_set('display_errors', 1);
require("Conn.php");
require("MySQLDao.php");
//$param=$_REQUEST['action'];
//echo json_encode($_GET);
//echo var_dump(json_encode($_GET));
$handle = fopen("php://input", "rb");
$param = $_REQUEST['action'];
while (!feof($handle)) {
$param .= fread($handle, 8192);
}
fclose($handle);
if (empty($param))
{
$returnValue["status"] = false;
$returnValue["title"] = "Error";
$returnValue["message"] = "No Data Recieved paige" .$param ."...";
echo json_encode($returnValue);
return;
}
else
{
$dao = new MySQLDao();
if ($dao->openConnection() == false)
{
$returnValue["status"] = false;
$returnValue["title"] = "Error";
$returnValue["message"] = "Connection Could Not Be Established Between Server And Database";
ob_clean();
echo json_encode($returnValue);
}
else
{
//Decodes data, dont change
$body = json_decode($param, true);
$recieved = $body["data"];
//Gets the result of a query
$result = $dao->getResults($recieved);
}
$dao->closeConnection();
//Return the result of the query
ob_clean();
//echo json_encode("param" .$param);
//echo json_encode("body" .$body);
//echo json_encode("recieved" .$recieved);
//echo json_encode("result" .$result);
echo "Your message : ", json_encode($result);
exit();
}
?>