Javascript AJAX无法生成正确的输出
因此,我尝试从javascript调用一个php方法,以便查询数据库并将结果输入到js功能中。目前,我的ajax中的Javascript AJAX无法生成正确的输出,javascript,php,mysql,ajax,Javascript,Php,Mysql,Ajax,因此,我尝试从javascript调用一个php方法,以便查询数据库并将结果输入到js功能中。目前,我的ajax中的console.log(output)正在输出: 字符串“{”action:“getResults”}(长度=23) 不确定它为什么这样做,它应该返回查询结果,这只是数据库中的一个条目 我的Javascript文件的一部分: function callPHP() { $.ajax({ type: "GET", datatype: "application/js
console.log(output)
正在输出:
字符串“{”action:“getResults”}(长度=23)
不确定它为什么这样做,它应该返回查询结果,这只是数据库中的一个条目
我的Javascript文件的一部分:
function callPHP() {
$.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)}
});
}
callPHP();
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 = '';
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";
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->MySQLDaoMethodName(parameters);
//Return the result of the query
//echo json_encode($result);
}
$dao->closeConnection();
return;
}
?>
Conn.php-这是所有连接信息,*出于保密原因:
<?php
error_reporting(E_ALL); ini_set('display_errors', 1);
class Conn
{
public static $dbhost = "***";
public static $dbname = "***";
public static $dbuser = "***";
public static $dbpass = "***";
}
?>
MySQLDao.php-此文件包含以下查询:
<?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($data)
{
$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 json_encode($result);
echo($result);
//return "yay";
}
}
?>
如何解决这个问题,只需将查询结果打印到网页上?我已经在这上面呆了很久了。欢迎并感谢您的帮助。提前谢谢
不太清楚它为什么这样做
它这样做是因为:
获取解析查询字符串的结果,将其转换为JSON字符串,然后通过var\u dump
传递该字符串(这会告诉您它是一个字符串,字符串是什么,以及它的长度)
它应该返回查询结果,这只是数据库中的一个条目
PHP的相关部分如下:
您尝试将$param
解析为JSON(这会失败,因为getResults
不是有效的JSON)。然后尝试从中读取数据
属性(失败是因为它从来没有数据)
如果要进行数据库查询,则需要实际查询数据库
如果您希望在该点获得一些输出,那么您需要提供一些输出
旁白:
- 该属性称为
notdataType
dataType
- 它接受值
,而不是“json”
“application/json”
- PHP当前返回纯文本(尽管它有一个
内容类型,因为您没有覆盖它),但它不能被解析为JSONtext/html
if (empty($param))
{
$returnValue["status"] = false;
$returnValue["title"] = "Error";
$returnValue["message"] = "No Data Recieved paige" .$param ."...";
echo json_encode($returnValue);//Only output
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";
echo json_encode($returnValue);//Only output
}
else
{
//Decodes data, dont change
$body = json_decode($param, true);
$recieved = $body["data"];
//Gets the result of a query
//$result = $dao->MySQLDaoMethodName(parameters);
//Return the result of the query
//echo json_encode($result);
//All of these are commented out
echo json_encode($result);//Fix
}
$dao->closeConnection();
return;
}
在回显页面上的任何内容之前,请确保设置了
内容类型
标题:
header('Content-Type: application/json; charset=UTF-8');
这样,客户机就知道需要JSON。您可能需要重命名该问题。这不是关于调用PHP函数,而是关于一个AJAX调用,该调用生成的输出不是您所期望的。好吧,您永远不会实际回显最后的
else
块。因此,如果$param
不是空的,并且$dao->openConnection()
是真实的,则不会返回任何内容。//如果(mysql_num_rows($result)==1){/$obj=mysql_fetch_object($result,'obResults'))
-我想知道您是否在尝试使用那些不与mysqli
api混合的mysql\uuu
函数。这与您的查询有什么不同?可能重复,因为PHP没有输出JSON,告诉浏览器期望JSON在这一阶段会适得其反。只有if(空)($param))
为true,而不是(或者如果$dao->openConnection()
返回false)我希望你不介意编辑。请看里面的评论。编辑:这里不是我的否决票,不知道你为什么会这样。@Fred ii--这听起来像是一个更好的建议,作为引号的样式表更改。
已经将其标记为引用的材料。好的,对不起。我习惯在引用某人时使用引号,这让我觉得se:“生存还是毁灭……”威廉·莎士比亚;-)编辑:见我对昆汀的评论;-)@BenM@Fred-ii-我同意昆汀的观点,引用的材料不需要强调(因此使用
,而不是
)。我已经取消了这些注释,所以现在是//解码数据,不要更改$body=json\u decode($param,true);$received=$body[“data”];//获取查询的结果$result=$dao->getResults($received);//返回查询echo json_encode的结果(“结果”..$result);但是它仍然没有生成我想要的内容更改后的输出是什么?字符串“{”action“:“getResults”}”(长度=23){“current_field”:null,“field_count”:null,“length”:null,“num_rows”:null,“type”:null}“结果”
datatype: "application/json",
if (empty($param))
{
$returnValue["status"] = false;
$returnValue["title"] = "Error";
$returnValue["message"] = "No Data Recieved paige" .$param ."...";
echo json_encode($returnValue);//Only output
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";
echo json_encode($returnValue);//Only output
}
else
{
//Decodes data, dont change
$body = json_decode($param, true);
$recieved = $body["data"];
//Gets the result of a query
//$result = $dao->MySQLDaoMethodName(parameters);
//Return the result of the query
//echo json_encode($result);
//All of these are commented out
echo json_encode($result);//Fix
}
$dao->closeConnection();
return;
}
header('Content-Type: application/json; charset=UTF-8');