Search code examples
perlmojolicious

Mojolicious API not showing JSON data


I'm trying to pass the following JSON object using AJAX, but can't see the data in the log file. How should I look for the 'data' part of my Ajax call in the API?

var jsObject = {
        myIds : [1234,5678],
        myType : 1 
};
$.ajax({
            type : "POST",
            url : "/myAddress",
            dataType: "json",
            data : JSON.stringify(jsObject),
            contentType : 'application/json',
            success: function(data)
            {
                console.log('Success', data);
            },
            error: function(data)
            {
              console.log('Failed', data);
            }
        });

Here is my API to show the data in the log file:

 sub my_sub {

    my $c = shift;

    my %params = %{$c->req->params};

    $c->app->log->debug(Dumper(\%params));
}

And this is what I see in the log file:

$VAR1 = {
      'charset' => 'UTF-8',
      'pairs' => []
};

Solution

  • Try this: $c->req->json;

    Read more in Mojolicious::Controller documentation.