$self->req->json is set from the message body, but a GET request has no body, only a URL (so setting content-type on a GET request doesn't really make sense - that is the content type of the message body).
jQuery's $.ajax() took whatever you put in the data field and sent it as a urlencoded query string, it generated a header from your contentType field but it ignored the dataType argument, which is why you need to JSON.stringify your data yourself.

If you really want to do this (you probably shouldn't), maybe define a helper in your app to decode JSON from the query string.


On 06/03/2014 06:22 AM, Baggio wrote:
Thanks for your quick response, but maybe I've done something wrongly, I still have no clues why I can't have the $self->req->json set, would you plase point out the mistakes I've made? thank you.
Here is the AJAX GET Request
$.ajax({
    url: '<%= url_for('/LessonList') %>',
    type: 'GET',
    contentType: 'application/json; charset=utf-8',
    dataType: 'json',
    data: JSON.stringify({ Grade: this.options[this.selectedIndex].text }),
}).done( function (data) {
    alert("Got The Lesson List: " + data);
} );

Here is the handler for the corresponding route

sub lessonList {
    my $ret;
    my $self = shift;
    my $m = $self->req->method;
    my $j = $self->req->json;

    $self->render( json => $j );
}

Here is the Content of $self->req, it seems that the data of the GET request is stored in $self->req->url->query but not $self->req->json
$VAR1 = bless( {
                 'events' => {},
                 'version' => '1.1',
                 'max_message_size' => 10485760,
                 'max_line_size' => 10240,
                 'method' => 'GET',
                 'cookies' => {},
                 'json' => undef,
                 'content' => bless( {
                                       'raw_size' => 372,
                                       'body' => 2,
                                       'read' => sub { "DUMMY" },
                                       'real_size' => 0,
                                       'pre_buffer' => '',
                                       'header_size' => 372,
                                       'state' => 'finished',
                                       'buffer' => '',
                                       'events' => {
                                                     'read' => [
                                                                 $VAR1->{'content'}{'read'}
                                                               ]
                                                   },
                                       'headers' => bless( {
                                                             'headers' => {
                                                                            'accept-encoding' => [
                                                                                                   [
                                                                                                     'gzip, deflate'
                                                                                                   ]
                                                                                                 ],
                                                                            'dnt' => [
                                                                                       [
                                                                                         '1'
                                                                                       ]
                                                                                     ],
                                                                            'host' => [
                                                                                        [
                                                                                          '127.1:3000'
                                                                                        ]
                                                                                      ],
                                                                            'content-type' => [
                                                                                                [
                                                                                                  'application/json; charset=utf-8'
                                                                                                ]
                                                                                              ],
                                                                            'accept-language' => [
                                                                                                   [
                                                                                                     'en-US,en;q=0.5'
                                                                                                   ]
                                                                                                 ],
                                                                            'referer' => [
                                                                                           [
                                                                                             'MailScanner warning: numerical links are often malicious: http://127.1:3000/lesson'
                                                                                           ]
                                                                                         ],
                                                                            'accept' => [
                                                                                          [
                                                                                            'application/json, text/_javascript_, */*; q=0.01'
                                                                                          ]
                                                                                        ],
                                                                            'x-requested-with' => [
                                                                                                    [
                                                                                                      'XMLHttpRequest'
                                                                                                    ]
                                                                                                  ],
                                                                            'connection' => [
                                                                                              [
                                                                                                'keep-alive'
                                                                                              ]
                                                                                            ],
                                                                            'user-agent' => [
                                                                                              [
                                                                                                'Mozilla/5.0 (X11; Linux x86_64; rv:29.0) Gecko/20100101 Firefox/29.0'
                                                                                              ]
                                                                                            ]
                                                                          },
                                                             'cache' => [],
                                                             'max_line_size' => 10240,
                                                             'normalcase' => {
                                                                               'referer' => 'Referer',
                                                                               'x-requested-with' => 'X-Requested-With'
                                                                             },
                                                             'state' => 'finished'
                                                           }, 'Mojo::Headers' ),
                                       'auto_upgrade' => 1,
                                       'asset' => bless( {
                                                           'content' => '',
                                                           'auto_upgrade' => 1
                                                         }, 'Mojo::Asset::Memory' ),
                                       'size' => 0
                                     }, 'Mojo::Content::Single' ),
                 'finished' => 1,
                 'raw_size' => 428,
                 'url' => bless( {
                                   'scheme' => undef,
                                   'fragment' => undef,
                                   'path' => bless( {
                                                      'path' => '/LessonList',
                                                      'charset' => 'UTF-8'
                                                    }, 'Mojo::Path' ),
                                   'base' => bless( {
                                                      'scheme' => 'http',
                                                      'port' => '3000',
                                                      'host' => '127.1'
                                                    }, 'Mojo::URL' ),
                                   'query' => bless( {
                                                       'string' => '{%22Grade%22:%225%22}'
                                                     }, 'Mojo::Parameters' )
                                 }, 'Mojo::URL' ),
                 'state' => 'finished'
               }, 'Mojo::Message::Request' );
--
You received this message because you are subscribed to the Google Groups "Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/d/optout.


--
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.

--
You received this message because you are subscribed to the Google Groups "Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/d/optout.

Reply via email to