Is there a very simple way to update a page in Perl Dancer using jQuery/AJAX?

346 views Asked by At

I have the following code in my main Dancer app .pm:

package Deadlands;
use Dancer ':syntax';
use Dice;

our $VERSION = '0.1';

get '/' => sub {
    my ($dieQty, $dieType, $bonus);
    my $button = param('button');
    $dieQty = param('dieQty');
    $dieType = param('dieType');
    $bonus = param('bonus');
    if (defined $dieQty && defined $dieType) {
        return Dice::Dice->new(dieType => $dieType, dieQty => $dieQty, bonus => $bonus)->getStandardResult();
    }
    template 'index';
};

true;

Here is my JavaScript:

$(document).ready(function() {
     $('#standardRoll').click(function() {
          $.get("/lib/Deadlands.pm", { button: '1', dieType: $("#dieType").val(), dieQty: $("#dieQty").val(), bonus: $("#bonus").val() }, processData);
          function processData(data) {
               $("#result").html(data);
          }
     });
});

I have a div in my web page called result that I want to be updated with the die roll result from Perl. Dancer keeps coming back with a 404 error in the command window when I push the submit button.

2

There are 2 answers

0
Quentin On BEST ANSWER

/lib/Deadlands.pm needs to be the URL of your route (probably / in this case), not the filesystem path of your Perl module.

0
hobbs On

Your AJAX request needs to point to a URL that actually exists, not a filename that has nothing to do with the web. Looks like $.get('/', ...) would do in this case.