0

I'm not sure at all if I've approached this correctly. I've created a script in Perl that takes some simple data and creates a simple json formatted output. When I run it local in the shell, I can see the output is correct using a print commmand. The file is called "dates.cgi" and runs locally from the cgi-bin directory. When I try to access the file directly on my local web server, I get a 500 error. It's not a web page, of course, just json output.

I figured that was a web server error. So I set up a standard jquery ajax call, but it too is failing.

Here is the Perl script that prints to terminal correctly:

#!/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)) {

    # Use a regular expression to ignore files beginning with a period
    next if ($file =~ m/^\./);

    # pluck out first 8 chars
    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);

# loop through array and create 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'm trying to access it this way from a webpage to load the data in:

// load data json
$.ajax({
    url: "cgi-bin/dates.cgi",
    async: false,
    success: function (json) {
        dates = json;
        alert(dates);
        alert("done");
    },
    fail: function () {
        alert("whoops");
    }
    dataType: "json"
});

It's just silently failing. Where do I look next?

pnuts
  • 58,317
  • 11
  • 87
  • 139
dbonneville
  • 2,079
  • 4
  • 19
  • 26
  • By *silently failing*, do you mean that the `alert("whoops");` is not being called? – mgamba Jan 19 '15 at 19:22
  • Yes, the rest of the page loads, 2 other ajax file loads execute correctly, but this ajax request with its alerts for success or fail are ignored. – dbonneville Jan 19 '15 at 19:26
  • is there supposed to be a comma before `dataType`? – mgamba Jan 19 '15 at 19:28
  • also, check the [jquery page for .ajax](http://api.jquery.com/jquery.ajax/). Looks like they chain `done`, `fail`, `always`, etc instead of specifying inside the first argument – mgamba Jan 19 '15 at 19:30
  • I'm not asking about json manipulation. Why was this marked duplicate? My question has nothing to do with the question Сухой27 posted. – dbonneville Jan 19 '15 at 19:50
  • Please check http://stackoverflow.com/questions/2165022/how-can-i-troubleshoot-my-perl-cgi-script – mpapec Jan 19 '15 at 19:57

2 Answers2

1

You should include the following to your perl file.

use CGI;
my $cgi = new CGI;

Then print proper header for json or just use text plain before anything is printed.

print "Content-Type: application/json", "\n\n";

print "Content-type: text/plain", "\n\n";
Asheliahut
  • 901
  • 6
  • 11
  • I added use CGI, etc from your suggestion. The shell output now looks like this: CGI=HASH(0x7fad440033f0) {'date':'20150112'}, {'date':'20150105'}, {'date':'20150101'} ]} – dbonneville Jan 19 '15 at 19:35
  • thats an odd output. it seems like you are outputting the CGI variable. That would not be what you want to output. You just want to output the header line and the json. – Asheliahut Jan 19 '15 at 19:56
0

Check the jquery documentation for .ajax. Looks like they chain done, fail, always, etc instead of specifying inside the first argument

Example,

$.ajax({
  url: "cgi-bin/dates.cgi",
  async: false,
  dataType: "json"
})
.done(function(data) {
  dates = data;
  alert(dates);
  alert("done");
})
.fail(function() {
  alert("whoops");
})
.always(function() {
  alert( "complete" );
});
mgamba
  • 1,189
  • 8
  • 10