Search code examples
javascriptjqueryajaxperldancer

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


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.


Solution

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