我使用AJAX POST的PHP響應有問題。AJAX POST的PHP錯誤響應
我的PHP代碼是這一個:
$vadb_dsn = 'odbc:DSN=VerticaDSNunixodbc;Database=DB';
$vadb_options = array(PDO::ATTR_AUTOCOMMIT=>FALSE, PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION);
$vadb_user = 'dbadmin';
$vadb_pass = 'dbpwd';
$user=$_POST['user'];
$pwd=$_POST['pwd'];
# Connect to Database
try {
$vadb = new PDO($vadb_dsn, $vadb_user, $vadb_pass, $vadb_options);
}
catch (PDOException $e) {
die("Couldn't connect to DB");
}
# Query to Database
$vsql = "SELECT user_name as 'user_name', password as 'password' from schema.table where user_name='".$user."' and password='".$pwd."';";
# Prepare Statement and Execute
$stmt = $vadb->prepare($vsql);
$stmt->execute();
# Fetch Results
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$result = $row['user_name'].";".$row['password'];
}
# Close Connection
$vadb = null;
# Output the result
echo $result
當我執行自檢,以這樣的方式
var url = "example.php";
var user= $('#user').val();
var pwd= $('#pwd').val();
$.ajax({
type: "POST",
url: url,
data: $("#formpost").serialize(), // serializes the form's elements.
success: function(data)
{
alert(data); // show response from the php script.
}
});
我得到的,因爲從PHP文件的響應,而不是查詢結果, PHP文件的代碼itstelf。 換句話說,我的網頁提示一個提示,其中的內容是php文件代碼... 有關如何解決此問題並獲得正確響應的任何想法?
直接調用PHP腳本(本身)是否運行查詢? –
如果您在頁面上獲取php代碼,請在頁面頂部使用<?php。 –
你有沒有試過[這](http://stackoverflow.com/questions/14918462/get-response-from-php-file-using-ajax)選項? –