简体   繁体   中英

AJAX call error 500 (Internal Server Error)

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.

Here is the 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:

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
    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.

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.

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

apt-get install php5-mysqlnd

and then restarting the server with

service apache2 restart

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

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

You cannot use isset like this

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();

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

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

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM