发送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控制器,用于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}}) { <-- 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->forward('homescreen'); <-- it highlights in bold this line
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或其他更合适的名称)
$c->req->body_data
,因为我在你上面展示的文档中看到了这行代码,但是当我使用它时,我会得到一个错误信息:“无法通过包“Catalyst::Request”定位对象方法“body_data”...”。而其他的例如$c->$req->body_parameters
则不会产生这个错误? - yonetpkbjiCatalyst
?自 5.90049_003 版本(2013 年 9 月 20 日)以来,body_data
方法是新的。 - mobbody_data
),或者使用$c->req->body
并自行解析JSON。 - mob