Javascript JSON_编码不输出任何内容
我想将一个php文件的查询结果发送到我的javascript,我使用了一个Javascript JSON_编码不输出任何内容,javascript,php,mysql,json,ajax,Javascript,Php,Mysql,Json,Ajax,我想将一个php文件的查询结果发送到我的javascript,我使用了一个AJAX,它似乎可以从我的BaseClass.php获取数据。但是,当使用JSON_encode时,它根本不输出任何内容。因此,我无法确定如何将一个php文件(MySQLDao.php)的查询结果发送到我的BaseClass.php,因此我可以将其发送到我的Javascript文件 我的代码: BaseClass.php: <?php error_reporting(E_ALL); ini_set('dis
AJAX
,它似乎可以从我的BaseClass.php
获取数据。但是,当使用JSON_encode
时,它根本不输出任何内容。因此,我无法确定如何将一个php文件(MySQLDao.php
)的查询结果发送到我的BaseClass.php
,因此我可以将其发送到我的Javascript文件
我的代码:
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);
exit();
}
?>
MySQLDao.php-此文件包含我要传递给我的js的查询结果
<?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 room.room_description FROM room WHERE room.room_id = 1";
$result = $this->mysqli->query($sql);
//if (mysql_num_rows($result) == 1) {
// $obj = mysql_fetch_object($result, 'obResults');
// echo($obj);
// return $obj;
//}
echo json_encode($result);
//echo($result);
//return false;
}
}
?>
感谢您的帮助,谢谢 检查控制台是否有错误。另外,您的输出不是有效的json。您应该将所有值放入一个数组中,并对该数组进行编码。您还应该有一个
出口
在echo之后停止脚本的执行并确保没有其他输出。echo“您的消息:”,json_encode($yourArray),“\n”json_enocde()
第一个参数应该是array
@JYoThIjson_encode
第一个参数可以是anything@OP:我建议在浏览器中打开/BaseClass.php?action=getResults
测试脚本,暂时不要使用AJAX。另外,为什么不使用$\u GET
?为什么打开输入流只是为了读取GET参数。。。?最后,您需要回显一个json_encode
,它应该有一个数组/对象作为参数。另外,您的输出不是有效的json。您应该将所有值放入一个数组中,并对该数组进行编码。您还应该有一个出口
在echo之后停止脚本的执行并确保没有其他输出。echo“您的消息:”,json_encode($yourArray),“\n”json_enocde()
第一个参数应该是array
@JYoThIjson_encode
第一个参数可以是anything@OP:我建议在浏览器中打开/BaseClass.php?action=getResults
测试脚本,暂时不要使用AJAX。另外,为什么不使用$\u GET
?为什么打开输入流只是为了读取GET参数。。。?最后,您需要回显一个json_encode
,它应该有一个数组/对象作为参数。
<?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 room.room_description FROM room WHERE room.room_id = 1";
$result = $this->mysqli->query($sql);
//if (mysql_num_rows($result) == 1) {
// $obj = mysql_fetch_object($result, 'obResults');
// echo($obj);
// return $obj;
//}
echo json_encode($result);
//echo($result);
//return false;
}
}
?>
$.ajax ({
type: "GET",
datatype: "application/json",
url: "BaseClass.php",
data: { action : 'getResults' },
//error: function(err){console.log(err)},
success: function(output) {
console.log(output);
//alert(output);
}
//error, function(err){console.log(err)}
});