简体   繁体   中英

js $.ajax calling perl, need to display perl which returns fetchall_arrayref

My $.ajax is calling a Perl file which returns data from MySQL in a fetchall_arrayref . How can i get the data back into ajax and put into the <select> dropdown options in the jQuery? it is returning something from perl, but the <select> dropdown displays the whole ajaxFunctions.pl file, char by char - each char is a selection. it needs to call the Perl file, do the mySQL SELECT there, and return the SELECT query result to the $.ajax "data" section so that I can show it in the GUI. I tested the MySQL query which works fine [returns 3 items, group names].

Here is the perl code:

if ($q->param('ajaxAction') eq "getGroups") {
my $return;
eval {
    my $query = qq(SELECT DISTINCT group FROM dbtable ORDER BY group);
    my $dbh = dbConnect();
    my $sth = $dbh->prepare($query);
    $sth->execute();
    $return = $sth->fetchall_arrayref();
};
if ($@) {
    $return = {'STATUS' => 'FAIL',
               'ERROR' => 'Error getting the group names. ' . $@};
}
print $q->header('application/json');
print to_json($return);
exit(0);
}

and here is the Javascript code:

function getGroups() {
$.ajax({
    type: 'POST',
    url: '/Project/metrics/cgi-bin/ajaxFunctions.pl',
    data: ({ ajaxAction: 'getGroups'}),
    success: function(data) {
        for(var i =0; i < data.length; i++) {
            $("#groupReportSelect").append('<option value="' + data[i] + '">&nbsp;&nbsp;' + data[i] + '</option>');
        }
        $('#groupReportSelect').multiselect();
        $("#groupReportSelect").multiselect("checkAll");
        getData();
    },
    error: function(request, status, err) {
        console.log('Error fetching groups. ' + err);
    }
}); 

}

Again, the output is each option in the select is an individual character of the whole perl file, starting with "#", "!", "/", of #!/usr/bin/perl , etc., etc.

Please let me know if I have left anything out. I have read several other posts, and am trying to give you all the appropriate data to answer my question.

You haven't configured your server to run the Perl program so it is returning it as plain text.

You'll need to consult the documentation for your webserver for how you execute the program.

You appear to be writing CGI (don't do that, write PSGI and then use a CGI handler to deploy if it you really need CGI) so use that as your search term in the manual.

Here is a detailed tutorial on how to do it with Apache .

Your AJAX request appears to be fetching the entire perl file listed in the object's url: property ( ie '/Project/metrics/cgi-bin/ajaxFunctions.pl', ) rather than the output from a REQUEST to that URI.

This is most likely due to your CGI and HTTP server environment being improperly set up. Your web server needs to have a CGI Handler set up to serve .pl files correctly as applications and not as files.

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