[英]AJAX call error 500 (Internal Server Error)
我有一個頁面,加載后會填充3個下拉菜單供您選擇。 在我的本地主機上,這可以正常工作,但是在我的實時服務器上,我在開發人員工具中看到錯誤“ POST(我的網站名稱)500(內部服務器錯誤)jquery.js:8630。
這是jQuery:
$(function() {
//after load, populate lists
Update_List_ProcedureAreas_Schedule();
Update_List_Patients_Cancel();
Update_List_Cancel_Reasons();
});
這是其中一個功能,但其他兩個功能幾乎相同,但所有三個功能均失敗。 我將要運行的函數發送到通用函數php文件中,並且該函數是否還有任何參數。 也許有點奇怪,但是它可以幫助我組織代碼:
function Update_List_Patients_Cancel() {
$.ajax({
url: 'php/functions.php',
type: 'POST',
dataType: 'json',
cache: false,
data: {Function_ID: 'pull_patient_list_cancel'},
success: function(data){
var dropDown = document.getElementById("selectedpatient_cancel");
dropDown.options.length=0;
dropDown[dropDown.length] = new Option('', '')
for (i = 0; i < data.length; i++) {
dropDown[dropDown.length] = new Option(data[i].Patient_Name, data[i].Patient_UID)
}
},
error: function() {
}
});
}
這是'php / functions.php'中的PHP,它使用Function_ID,然后使用開關來驗證輸入,然后使用任何適當的變量調用函數:
<?php
session_start();
//Make sure a funciton was sent
if(isset($_POST['Function_ID']) == true) {$tempFunction = $_POST['Function_ID'];}
else {echo "No fuction selected";}
//switch to functions
switch($tempFunction) {
case 'pull_patient_list_cancel':
pull_patient_list_cancel();
break;
}
//Pull patient list for cancel drop down
function pull_patient_list_cancel() {
require $_SESSION['db_connect']; //code to connect to the database, which is successful, btw
$returnArray = array();
$sql = "
SELECT Patient_UID, Patient_Name
FROM tbl_patients
WHERE Active = 1
";
if($result = $conn->query($sql)) {
$conn->close();
foreach($result->fetch_all(MYSQLI_ASSOC) as $row) {
$returnArray[] = $row;
}
echo json_encode($returnArray);
} else {
$conn->close();
echo "Failed - pull patient lists cancel";
}
}
看起來由於某種原因,我無法在foreach循環中執行。 我放入了虛假的回聲,以查看該函數在哪里停止運行,並且不會在for循環內或之后輸出任何回聲,但是它將一直循環到該循環開始。 我已經檢查了開發工具中的響應日志,並看到虛假的回聲一直傳遞到foreach循環,然后什么也沒有。
有任何想法嗎?
- - - - - - 解 - - - - - - - - -
問題是我在本地主機xampp安裝程序上安裝了mysqlnd,但今天沒有安裝在我的digitalocean lampp服務器上。 這樣就使fetch_all(MYSQLI)無法正常工作。 但是,我可以堅持使用mysql安裝並使用fetch_assoc(),它可以正常工作。
但是,我最終使用以下命令在我的digitalocean Droplet上安裝了mysqlnd:
apt-get安裝php5-mysqlnd
然后使用以下命令重新啟動服務器
服務apache2重新啟動
現在我所有的fetch_all(MYSQLI)函數都可以正常工作。 感謝大家的幫助。
您的PHP函數頁面上有語法錯誤。
您不能使用像這樣的isset
從更改您的代碼
if(isset($_POST['Function_ID']) == true) //which is wrong
至
if(isset($_POST['Function_ID']) && $_POST['Function_ID'])==true)
// this is the right way
執行完所有程序並關閉連接后,
else{}
$conn->close();
在獲取foreach的結果之前,無法關閉連接。 循環后將連接關閉。
更好的是,刪除兩個連接關閉語句,然后在else塊之后放置一個。
if($result = $conn->query($sql)) {
foreach($result->fetch_all(MYSQLI_ASSOC) as $row) {
$returnArray[] = $row;
}
echo json_encode($returnArray);
} else {
echo "Failed - pull patient lists cancel";
}
$conn->close();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.