Ajax在Javascript中产生错误的输出

时间:2017-08-16 20:43:34

标签: javascript php jquery ajax

我无法理解为什么我的ajax输出错误的代码。它输出的css负载,我相信它来自我的'MySQLDao.php'文件,但我不确定。代码如下。有人说这是因为“data:{action:'getResults'}”行被忽略了,但我不知道如何制作它所以它不会被忽略。任何帮助表示赞赏。谢谢!

我的Javascript代码的一部分:

function callPHP() {
    $.ajax ({
        type: "GET",
        datatype: "application/json",
        url: "MySQLDao.php",
        data: { action : 'getResults' },
        success: function(output) {

            alert(output);

        }

    });

}

callPHP();  

我的PHP文件:

<?php

    require("Conn.php");

    header("Access-Control-Allow-Origin: *");
    error_reporting(E_ALL); ini_set('display_errors', 1);

    //Class for holding queries
    class MySQLDao
    {
        private $dbhost = null;
        private $dbuser = null;
        private $dbpass = null;
        private $mysqli = null;
        private $dbname = null;
        private $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 generateRoomID()
        {
            $sql = "INSERT INTO room (room_id) VALUES (null);";
            $result = $this->mysqli->query($sql);
            if ($result == true)
            {
                $toReturn["status"] = true;
                $toReturn["roomID"] = $this->mysqli->insert_id;
                return $toReturn;
            }
            else
            {
                $toReturn["status"] = false;
                $toReturn["message"] = mysql_error($this->mysqli);
                return $toReturn;
            }
        }

        public function saveRoom($data)
        {
            $roomID = $data[0];
            $roomDescription = $data[1];
            $columns = $data[2];
            $rows = $data[3];

            $this->mysqli->autocommit(FALSE);

            $this->mysqli->query("UPDATE room SET room_description='".$roomDescription."' WHERE room_id='".$roomID."';");

            for ($i = 0; $i<count($columns); $i++)
            {
                for ($j = 1; $j<=$rows[$i]; $j++)
                {
                    $currentLabel = "{$columns[$i]}{$j}";
                    $this->mysqli->query("INSERT INTO shelf (shelf_label) VALUES ('".$currentLabel."');");
                    $shelfID = $this->mysqli->insert_id;
                    $this->mysqli->query("INSERT INTO room_shelf (room_id, shelf_id) VALUES ('".$roomID."','".$shelfID."');");
                }
            }

            if ($this->mysqli->commit())
            {
                $toReturn["status"] = true;
                $toReturn["message"] = "Room Created";
                return $toReturn;
            }
            else
            {
                $this->mysqli->rollback();
                $toReturn["status"] = false;
                $toReturn["message"] = "SQL Error";
                return $toReturn;
            }
        }

        public function updateShelf($data)
        {
            $shelfID = $data[0];
            $itemName = $data[1];
        }

        public function getRoomDetails($data)
        {
            $roomID = $data[0];

            $sql = "SELECT room.room_description, shelf.shelf_label, shelf.shelf_id FROM room INNER JOIN room_shelf ON room.room_id=room_shelf.room_id INNER JOIN shelf ON shelf.shelf_id=room_shelf.shelf_id WHERE room.room_id='".$roomID."';";

            $result = $this->mysqli->query($sql);

            if (mysqli_num_rows($result) > 0)
            {
                $toReturn["status"] = true;
                $toReturn["message"] = "Room Found";
                $toReturn["room_description"] = $row['room_description'];
                $shelves = [];

                foreach ($result as $row)
                {
                    $currentShelf["shelf_label"] = $row['shelf_label'];
                    $currentShelf["shelf_id"] = $row['shelf_id'];
                    array_push($shelves, $currentShelf);
                }
                $toReturn["shelves"] = $shelves;
                return $toReturn;
            }
            else
            {
                $toReturn["status"] = false;
                $toReturn["title"] = "Error";
                $toReturn["message"] = "Room Not Found";
                return $toReturn;
            }

        }

        public function getResults($data)
        {

            $sql = "SELECT room.room_description FROM room WHERE room.room_id = 1";

            $result = $this->mysqli->query($sql);

            echo json_encode($result);

        }

    }

    $daoObj = new MySQLDao();
    $daoObj->getResults();

?>

我的康恩班:

<?php
    class Conn
    {
        public static $dbhost = "xxx";
        public static $dbname = "xxx";
        public static $dbuser = "xxx";
        public static $dbpass = "xxx";
    }
?>

My BaseClass:

<?php
    require("Conn.php");
    require("MySQLDao.php");

    $handle = fopen("php://input", "rb");
    $raw_post_data = '';
    while (!feof($handle)) {
        $raw_post_data .= fread($handle, 8192);
    }
    fclose($handle);

    if (empty($raw_post_data))
    {
        $returnValue["status"] = false;
        $returnValue["title"] = "Error";
        $returnValue["message"] = "No Data Recieved";
        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($raw_post_data, 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;
    }
?>

0 个答案:

没有答案
相关问题