I'm feeling uncertain about whether or not I have gone about this the right way. I have crafted a Perl script that takes basic data and generates a JSON formatted output. Testing it locally in the shell shows that the output is correct when using a print command. The script, named "dates.cgi," runs from the cgi-bin directory on my local machine without any issues. However, attempting to access the file directly on my local web server results in a 500 error. This file does not serve as a webpage; instead, it outputs JSON data.
I suspected the issue was with the web server, so I set up a standard jQuery AJAX call, but unfortunately, it is also unsuccessful.
Below is the Perl script that successfully prints to the terminal:
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
my $dir = '../data';
my $json;
my @dates;
opendir(DIR, $dir) or die $!;
while (my $file = readdir(DIR)) {
# Ignore files starting with a period using a regular expression
next if ($file =~ m/^\./);
# Extract the first 8 characters
my $temp = substr $file, 0, 8;
# Populate array
push(@dates, $temp);
}
closedir(DIR);
# Sort high to low
@dates = sort { $b <=> $a } @dates;
# print Dumper (@dates);
# Iterate through the array and create the inner section of JSON
my $len = @dates;
my $x = 0;
foreach (@dates){
if ($x < $len-1){
$json .= "\t{'date':'$_'},\n";
} else {
$json .= "\t{'date':'$_'}\n";
}
$x++;
}
# Add JSON header and footer
$json = "{'dates':[\n" . $json;
$json .= "]}";
# Print
print "$json\n";
I am attempting to access this data from a webpage to load it:
// Load JSON data
$.ajax({
url: "cgi-bin/dates.cgi",
async: false,
success: function (json) {
dates = json;
alert(dates);
alert("done");
},
fail: function () {
alert("whoops");
}
dataType: "json"
});
The process is failing silently. What should I investigate next?