鍍金池/ 問答/PHP  HTML/ 如何將php文件放到阿里云服務(wù)器上,使網(wǎng)頁發(fā)送ajax請求能正常返回內(nèi)容?

如何將php文件放到阿里云服務(wù)器上,使網(wǎng)頁發(fā)送ajax請求能正常返回內(nèi)容?

現(xiàn)在項(xiàng)目有一個(gè)需求是填寫表單,然后使用ajax發(fā)送表單內(nèi)容到php中,php經(jīng)過處理后將這些表單內(nèi)容寫入阿里云數(shù)據(jù)庫中.我現(xiàn)在將網(wǎng)頁文件index.html放入了/test文件夾下,里面有發(fā)送的ajax請求:

   $('#submit-btn').click(function (e) {
        e.preventDefault();
        $.ajax({
            url: "http://test.com/test/php/test.php",
            type: "POST",
            dataType:"text",
            data:
                $('#form').serialize(),
            success: function (data, textStatus) {
                console.log(data);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                console.log(XMLHttpRequest.status);
                console.log(XMLHttpRequest.readyState);
                console.log(textStatus);
            }
        }).done(function () {
            console.log("success");
        }).fail(function () {
            console.log("error");
        }).always(function () {
            console.log("complete");
        });
    });

我把接收此請求的test.php放在了/test/php文件夾下.此文件是實(shí)例化了一個(gè)封裝類,封裝類中的數(shù)據(jù)庫連接為:

    private $hostname = 'test.com';
    private $username = 'root';

    private $password = '正確密碼'; 
    private $dbName   = '數(shù)據(jù)庫名'; 

當(dāng)在網(wǎng)頁打開時(shí)卻出現(xiàn):
500 (Internal Server Error);
控制臺(tái)其他信息是:
500
4
error
error
complete.
請問是什么情況造成這種錯(cuò)誤?在本地xampp上是測試通過的,可以向本地的數(shù)據(jù)表中寫入相關(guān)的數(shù)據(jù).
使用的是阿里云的服務(wù)器和數(shù)據(jù)庫.
我覺著好像是我連接數(shù)據(jù)庫過程出現(xiàn)問題了.在阿里云中可以使用php連接數(shù)據(jù)么?我的類封裝類文件是:

<?php

// 以PDO的形式封裝類,并添加查詢等sql語句
class DB
{
    /*** mysql hostname ***/
    private $hostname = 'test.com'; // Put your host name here
    /*** mysql username ***/
    private $username = 'root'; // Put your MySQL User name here
    /*** mysql password ***/
    private $password = '正確密碼'; // Put Your MySQL Password here
    /*** mysql database ***/
    private $dbName   = '數(shù)據(jù)庫名'; // Put Your MySQL Database name here
    /*** database resource ***/
    public $dbh = NULL; // Database handler
    //默認(rèn)構(gòu)造函數(shù)
    public function __construct() // Default Constructor
    {
        try
        {
            $this->dbh = new PDO("mysql:host=$this->hostname;dbname=$this->dbName", $this->username, $this->password);
            /*** echo a message saying we have connected ***/
            // echo 'Connected to database'; // Test with this string
        }
        catch(PDOException $e)
        {
            echo __LINE__.$e->getMessage();
        }
    }
    //設(shè)置關(guān)閉連接
    public function __destruct()
    {
        $this->dbh = NULL; // Setting the handler to NULL closes the connection propperly
    }
    //設(shè)置具體數(shù)據(jù)庫交互業(yè)務(wù)
    public function runQuery($sql)
    {
        try
        {
            //echo $sql;
            $count = $this->dbh->exec($sql) or print_r($this->dbh->errorInfo());
            return  $count;
        }
        catch(PDOException $e)
        {
            echo __LINE__.$e->getMessage();
        }
    }
    //從數(shù)據(jù)庫里查詢數(shù)據(jù)并返回?cái)?shù)據(jù)數(shù)組
    public function getQuery($sql)
    {
        $stmt = $this->dbh->query($sql);
        $stmt->setFetchMode(PDO::FETCH_ASSOC);
        return $stmt; // Returns an associative array that can be diectly accessed or looped through with While or Foreach
    }
}
?>

然后再text.php中實(shí)例化一個(gè)DB:include('DB.php')(它們在同一文件夾下):

//實(shí)例化類
$dataBase = new DB;
回答
編輯回答
不歸路

private $hostname = 'test.com';
'test.com' ?? 這是你本地測試的域名把。

2017年8月23日 02:10
編輯回答
舊時(shí)光

url: "http://test.com/test/php/test.php",這里換成相對路徑php/test.php

2018年5月8日 05:36
編輯回答
做不到

將域名換成你阿里云的IP

2018年2月28日 17:38
編輯回答
朽鹿

這種問題只能自己一步一步調(diào)試,不難調(diào)出來的,500錯(cuò)誤應(yīng)該服務(wù)器出問題了,檢查一下代碼。

2018年7月31日 05:21