AJAX调用错误500(内部服务器错误)

时间:2021-03-10 15:39:38

I have a page that, upon loading, fills 3 drop down menus for uses to select from. On my localhost, this works fine, but on my live server I get the error in Developer Tools "POST (my site's name) 500 (Internal Server Error) jquery.js:8630.

我有一个页面,加载后,填充3个下拉菜单供用户选择。在我的localhost上,这工作正常,但在我的实时服务器上,我在开发人员工具中收到错误“POST(我的站点名称)500(内部服务器错误)jquery.js:8630。

Here is the jquery:

这是jquery:

$(function() {
    //after load, populate lists
    Update_List_ProcedureAreas_Schedule();
    Update_List_Patients_Cancel();
    Update_List_Cancel_Reasons();
});

Here's one of the functions, but the other two are nearly identical, but all 3 fail. I send to a generic functions php file the function that I want to run, and if there are any parameters with the function as well. Maybe be a bit weird, but it helps me organize my code:

这是其中一个功能,但其他两个几乎完全相同,但所有3个都失败了。我发送一个通用函数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() {

        }
    });
}

Here is the PHP in 'php/functions.php' which takes the Function_ID and then uses a switch to verify the input and then call the function with any appropriate variables:

这里是'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";
    }
}

It looks like for some reason, I'm not able to execute in the foreach loop. I've put in bogus echos to see where the function stops running, and it won't output any echo within the for each loop or after it, but it will up until that loop starts. I've check the response log in Development Tools and see the bogus echos coming through until the foreach loop, and then nothing.

看起来由于某种原因,我无法在foreach循环中执行。我已经放入虚假的回声来查看函数停止运行的位置,并且它不会在每个循环内或之后输出任何回声,但它会一直运行直到该循环开始。我已经检查了开发工具中的响应日志,看到伪造的回声直到foreach循环,然后什么都没有。

Any ideas?

------------Solution-----------------

The problem was that I had mysqlnd installed on my localhost xampp setup, but it did not install on my digitalocean lampp server that I set up today. So that made the fetch_all(MYSQLI) not work. However, I could stick with the mysql installation and use fetch_assoc() and it worked fine.

问题是我在我的localhost xampp设置上安装了mysql,但它没有安装在我今天设置的digitalocean lampp服务器上。这使得fetch_all(MYSQLI)不起作用。但是,我可以坚持使用mysql安装并使用fetch_assoc()并且它工作正常。

But, I ended up installing mysqlnd on my digitalocean droplet by using:

但是,我最终使用以下命令在我的digitalocean droplet上安装mysqlnd:

apt-get install php5-mysqlnd

apt-get install php5-mysqlnd

and then restarting the server with

然后使用重新启动服务器

service apache2 restart

service apache2 restart

and now all of my fetch_all(MYSQLI) functions work. Thanks everyone for your help.

现在我的所有fetch_all(MYSQLI)函数都能正常工作。谢谢大家的帮助。

2 个解决方案

#1


1  

You cannot close the connection before you fetch the results for the foreach. Move the connection close after the loop.

在获取foreach的结果之前,无法关闭连接。循环后关闭连接。

Better still, remove both connection close statements, and put a single one after the else block.

更好的是,删除两个连接关闭语句,并在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();

#2


1  

There is syntax error on your PHP function page....

您的PHP函数页面上存在语法错误....

You cannot use isset like this

你不能像这样使用isset

change your code from

改变你的代码

    if(isset($_POST['Function_ID']) == true) //which is wrong 

To

     if(isset($_POST['Function_ID']) && $_POST['Function_ID'])==true) 
   // this is the right way 

Connection should be closed after all the program has executed and

在所有程序执行完毕后,应该关闭连接

   else{} 
   $conn->close();

#1


1  

You cannot close the connection before you fetch the results for the foreach. Move the connection close after the loop.

在获取foreach的结果之前,无法关闭连接。循环后关闭连接。

Better still, remove both connection close statements, and put a single one after the else block.

更好的是,删除两个连接关闭语句,并在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();

#2


1  

There is syntax error on your PHP function page....

您的PHP函数页面上存在语法错误....

You cannot use isset like this

你不能像这样使用isset

change your code from

改变你的代码

    if(isset($_POST['Function_ID']) == true) //which is wrong 

To

     if(isset($_POST['Function_ID']) && $_POST['Function_ID'])==true) 
   // this is the right way 

Connection should be closed after all the program has executed and

在所有程序执行完毕后,应该关闭连接

   else{} 
   $conn->close();