账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    如何将php文件放到阿里云服务器上,使网页发送ajax请求能正常返回内容?
    24
    0

    现在项目有一个需求是填写表单,然后使用ajax发送表单内容到php中,php经过处理后将这些表单内容写入阿里云数据库中.我现在将网页文件index.html放入了/test文件夹下,里面有发送的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文件夹下.此文件是实例化了一个封装类,封装类中的数据库连接为:

        private $hostname = 'test.com';
        private $username = 'root';
    
        private $password = '正确密码'; 
        private $dbName   = '数据库名'; 

    当在网页打开时却出现:
    500 (Internal Server Error);
    控制台其他信息是:
    500
    4
    error
    error
    complete.
    请问是什么情况造成这种错误?在本地xampp上是测试通过的,可以向本地的数据表中写入相关的数据.
    使用的是阿里云的服务器和数据库.
    我觉着好像是我连接数据库过程出现问题了.在阿里云中可以使用php连接数据么?我的类封装类文件是:

    <?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   = '数据库名'; // Put Your MySQL Database name here
        /*** database resource ***/
        public $dbh = NULL; // Database handler
        //默认构造函数
        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();
            }
        }
        //设置关闭连接
        public function __destruct()
        {
            $this->dbh = NULL; // Setting the handler to NULL closes the connection propperly
        }
        //设置具体数据库交互业务
        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();
            }
        }
        //从数据库里查询数据并返回数据数组
        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中实例化一个DB:include('DB.php')(它们在同一文件夹下):

    //实例化类
    $dataBase = new DB;
    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    • 星野道 普通会员 1楼
      502 Bad Gateway

      502 Bad Gateway


      nginx
    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部