AJAX没有产生正确的输出

时间:2017-08-23 14:21:23

标签: javascript php mysql ajax

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

  

string'{“action”:“getResults”}'(length = 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";

        }

    }
?>

如何解决这个问题,只需将查询结果打印到网页上即可?多年来我一直坚持这个。任何帮助都是受欢迎和赞赏的。提前谢谢。

3 个答案:

答案 0 :(得分:1)

  

不确定为什么要这样做

因为这样做:

echo var_dump(json_encode($_GET));

您获取解析查询字符串的结果,将其转换为JSON字符串,然后将该字符串传递给var_dump(它告诉您它是一个字符串,字符串是什么,以及它是多长时间)。

  

它应该返回查询结果,这只是数据库中的一个条目

PHP的相关部分是:

   $body = json_decode($param, true);
   $recieved = $body["data"];

您尝试将$param解析为JSON(失败,因为getResults无效JSON)。然后尝试从中读取data属性(失败,因为它从来没有)。

如果要进行数据库查询,则需要实际查询数据库。

如果你想在那一点输出一些,那么你需要提供一些输出。

除了:

datatype: "application/json",
  • 该属性称为dataType而不是datatype
  • 它接受值"json",而不是"application/json"
  • PHP目前返回纯文本(虽然它有text/html内容类型,因为你没有覆盖它),但不能将其解析为JSON

答案 1 :(得分:1)

如果因为你从不使用echo

而出现错误,那么你的BaseClass只会输出
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;
}

答案 2 :(得分:-2)

确保在页面上回显任何之前设置MoreLINQ标题:

Content-type

这样客户就知道期待JSON。

相关问题