title: Perl Catalyst - Obtaining JSON data from AJAX POST request
body: <p>I am trying to obtain the JSON data that is sent to the server via an AJAX POST when a button is clicked on my <code>homescreen</code> page on my Catalyst application:</p>
  
  <p>The AJAX POST that sends the JSON data to the server:</p>
  
  <pre><code>$("#saveCanvasStates").click(function () {
  // button to save canvas states to a database
  
  // Serialize the states array
  var JsonStringForTransport = JSON.stringify({stateForUserNumber7: states});
  
  // POST the JSON to the server
  var thePost = $.ajax({
      url: 'homescreen',
      type: 'POST',
      dataType: 'json',
      data: JsonStringForTransport,
      contentType: 'application/json; charset=utf-8'
  });
  </code></pre>
  
  <p>I also have the following Catalyst Controller for the <code>homescreen</code> page which the button that sends the AJAX POST is located on:</p>
  
  <p><strong>Catalyst Controller:</strong></p>
  
  <pre><code>package MyProject::Controller::Homescreen;
  
  use strict;
  use warnings;
  use parent 'Catalyst::Controller';
  use Data::Dumper;
  
  __PACKAGE__-&gt;config-&gt;{namespace} = '';
  
  sub homescreen :Path('/homescreen') :Args(0)  {
  
          my ( $self, $c ) = @_;
  
          $c-&gt;stash({title =&gt; 'Home Screen',
                     pagetype =&gt; 'html',
                     template =&gt; 'homescreen.html'
                   });
  
  
  #here is where I think I need to obtain the JSON data from the AJAX POST request 
  #and save it to my database
  
  }
  
  1;
  </code></pre>
  
  <p>Once I have this JSON data in a form I can work with I will then be saving it to a Postgres database.</p>
  
  <p>From looking at the CPAN docs for <a href="http://search.cpan.org/~jjnapiork/Catalyst-Runtime-5.90051/lib/Catalyst/Request.pm#$req-%3Ebody_parameters" rel="nofollow noreferrer">Catalyst::Request</a>, as it's my understanding this is what to refer to when dealing with request stuff, it's possible to use the following to do stuff with AJAX POST data?:</p>
  
  <ul>
  <li>$c->$req->body_data</li>
  <li>$c->$req->body_parameters</li>
  <li>$c->$req->body_params</li>
  </ul>
  
  <p>But I am unsure about the best way to get the data into a form I can then insert into my database and which one of the methods should be used in preference? </p>
  
  <p>I can find very little documentation that has helped me.</p>
  
  <h2>Update (relating to RET's answer)</h2>
  
  <p>I definitely have <code>body_data</code> to display because when I do:</p>
  
  <p><code>print Dumper($c-&gt;req-&gt;body_data);</code></p>
  
  <p>I get the following printed in my development server log:</p>
  
  <pre><code>$VAR1 = {
            'stateForUserNumber7' =&gt; [
                                       {
                                         'width' =&gt; 102,
                                         'offsetY' =&gt; 56,
                                         'x' =&gt; 11,
                                         'height' =&gt; 102,
                                         'image' =&gt; {},
                                         'y' =&gt; 14,
                                         'contextIndex' =&gt; 2,
                                         'dragging' =&gt; bless( do{\(my $o = 0)}, 'Cpanel::JSON::XS::Boolean' ),
                                         'offsetX' =&gt; 73
                                       },
                                       {
                                         'width' =&gt; 102,
                                         'offsetY' =&gt; 34,
                                         'x' =&gt; 103,
                                         'height' =&gt; 102,
                                         'image' =&gt; {},
                                         'y' =&gt; 17,
                                         'contextIndex' =&gt; 3,
                                         'dragging' =&gt; $VAR1-&gt;{'stateForUserNumber7'}[0]{'dragging'},
                                         'offsetX' =&gt; 46
                                       }
                                     ]
          };
  [info] *** Request 15 (1.250/s) [17427] [Fri Dec  6 00:02:22 2013] ***
  [debug] Path is "homescreen"
  [debug] "POST" request for "homescreen" from "192.168.1.100"
  [debug] Rendering template "homescreen.html"
  [debug] Response Code: 200; Content-Type: text/html; charset=utf-8; Content-Length: 7010
  [info] Request took 0.025343s (39.459/s)
  .------------------------------------------------------------+-----------.
  | Action                                                     | Time      |
  +------------------------------------------------------------+-----------+
  | /homescreen                                                | 0.014044s |
  | /end                                                       | 0.001992s |
  |  -&gt; Organiser::View::TT-&gt;process                           | 0.001058s |
  '------------------------------------------------------------+-----------'
  </code></pre>
  
  <h2>Further update</h2>
  
  <p>This is the error it gives in the development server output when using <code>-d</code>:</p>
  
  <pre><code>Caught exception in Organiser::Controller::Homescreen-&gt;homescreen "Can't use an undefined value as a HASH reference at /home/fred/Organiser/script/../lib/Organiser/Controller/Homescreen.pm line 21."
  </code></pre>
  
  <p>This is the error I get from Stack Trace when running the development server:</p>
  
  <pre><code>Stack Trace
  Package                             Line    File
  Organiser::Controller::Homescreen   21    /home/fred/Organiser/lib/Organiser/Controller/Homescreen.pm
  
  18: 
  19: print STDERR Dumper $c-&gt;req-&gt;body_data; 
  20: 
  21: foreach my $data (@{$c-&gt;req-&gt;body_data-&gt;{stateForUserNumber7}}) {     &lt;-- it highlights in bold this line
  22:      print "DOLLAR DATA $data\n"; 
  23: } 
  24:
  
  Organiser::Controller::Root     17  /home/fred/Organiser/lib/Organiser/Controller/Root.pm
  
  14: sub index :Path :Args(0) { 
  15: my ( $self, $c ) = @_; 
  16: 
  17: $c-&gt;forward('homescreen');       &lt;-- it highlights in bold this line
  18: 
  19: } 
  20: 
  </code></pre>
  
  <p>Using Firebug this is the POST request that is occurring (after I comment out the foreach that is making it error)</p>
  
  <pre><code>Source
  {"stateForUserNumber7":[{"dragging":false,"contextIndex":4,"image":{},"x":108,"y":4,"width":102,"height":102,"offsetX":45,"offsetY":65}]}
  </code></pre>
  
  <p>It is always <code>stateForUserNumber7</code> (I should have named it master_user or something really)</p>