gpt4 book ai didi

javascript - AJAX 未输出正确的内容

转载 作者:行者123 更新时间:2023-11-29 18:29:33 24 4
gpt4 key购买 nike

所以我尝试从 javascript 调用 php 方法,以便我可以查询数据库并将结果获取到我的 js 功能中。目前,我的 ajax 中的 'console.log(output)' 只是输出:

"array (size=1)
'action' => string 'getResults' (length=10)'"

不太确定为什么要这样做,它应该返回查询结果,这只是数据库中的一项。有人有什么想法吗?欢迎任何帮助!谢谢。

我的 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 var_dump($_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);

}

}
?>

最佳答案

我认为您误解了如何在 javscript 和 php 之间传输数据。

  1. 如果您想发送对象或数据数组,您的 javascript 应该使用 $.post() 进行发布。
  2. 您的 php 将从 javascript 接收 json。你需要使用php的json_decode函数来使其对php有用。
  3. 如果您希望 php 脚本的输出对 javascript 有用,则需要使用 php 的 json_encode 函数对其进行编码,然后再将其返回到调用脚本。

http://php.net/manual/en/function.json-decode.php

关于javascript - AJAX 未输出正确的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45827579/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com