现在项目有一个需求是填写表单,然后使用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;