我试图获取在homescreenCatalyst应用程序的页面上单击按钮时通过AJAX POST发送到服务器的JSON数据:
将JSON数据发送到服务器的AJAX POST:
$("#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'
});
我还有以下Catalyst Controller用于homescreen发送AJAX POST的按钮所在的页面:
Catalyst控制器:
package MyProject::Controller::Homescreen;
use strict;
use warnings;
use parent 'Catalyst::Controller';
use Data::Dumper;
__PACKAGE__->config->{namespace} = '';
sub homescreen :Path('/homescreen') :Args(0) {
my ( $self, $c ) = @_;
$c->stash({title => 'Home Screen',
pagetype => 'html',
template => '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;
一旦我将这个JSON数据放在一个可以使用的表单中,我将把它保存到Postgres数据库中.
从查看Catalyst :: Request的CPAN文档,因为我的理解是这是在处理请求时引用的内容,可以使用以下内容来处理AJAX POST数据?:
$ C - > $ req-> body_data
$ C - > $ req-> body_parameters
$ C - > $ req-> body_params
但我不确定将数据转换为表格的最佳方法,然后我可以将其插入到我的数据库中,并且应该优先使用哪种方法?
非常感谢您对此的帮助,因为我能找到很少帮助我的文档,非常感谢.
-----更新-----(与RET的答案有关)
我必须body_data要显示,因为我这样做:
print Dumper($c->req->body_data);
我在开发服务器日志中打印了以下内容:
$VAR1 = {
'stateForUserNumber7' => [
{
'width' => 102,
'offsetY' => 56,
'x' => 11,
'height' => 102,
'image' => {},
'y' => 14,
'contextIndex' => 2,
'dragging' => bless( do{\(my $o = 0)}, 'Cpanel::JSON::XS::Boolean' ),
'offsetX' => 73
},
{
'width' => 102,
'offsetY' => 34,
'x' => 103,
'height' => 102,
'image' => {},
'y' => 17,
'contextIndex' => 3,
'dragging' => $VAR1->{'stateForUserNumber7'}[0]{'dragging'},
'offsetX' => 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 |
| -> Organiser::View::TT->process | 0.001058s |
'------------------------------------------------------------+-----------'
-----进一步更新-----
这是使用-d时在开发服务器输出中给出的错误
Caught exception in Organiser::Controller::Homescreen->homescreen "Can't use an undefined value as a HASH reference at /home/fred/Organiser/script/../lib/Organiser/Controller/Homescreen.pm line 21."
这是我在运行开发服务器时从Stack Trace获得的错误
Stack Trace
Package Line File
Organiser::Controller::Homescreen 21 /home/fred/Organiser/lib/Organiser/Controller/Homescreen.pm
18:
19: print STDERR Dumper $c->req->body_data;
20:
21: foreach my $data (@{$c->req->body_data->{stateForUserNumber7}}) {
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->forward('homescreen');
18:
19: }
20:
使用Firebug这是发生的POST请求(在我注释掉正在使其出错的foreach之后)
Source
{"stateForUserNumber7":[{"dragging":false,"contextIndex":4,"image":{},"x":108,"y":4,"width":102,"height":102,"offsetX":45,"offsetY":65}]}
总是stateForUserNumber7(我应该将它命名为master_user或者其他东西)