[当我在Catalyst应用程序的homescreen
页面上单击按钮时,我试图获取通过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'
});
我在homescreen
页面上也具有以下Catalyst Controller,发送AJAX POST的按钮位于:
催化剂控制器:
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数据吗?:
但是我不确定将数据转换为可以插入到数据库中的表单的最佳方法,应该优先使用哪种方法?
我几乎找不到对我有帮助的文档。
我肯定要显示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
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或其他名称)
我的其他答案是(希望)对调试Catalyst / JSON问题的任何人有用的信息。但是随后对该问题的更新表明这里的问题实际上完全不同。
尽管在使日志持久保存并进行分析时,内置的日志记录功能非常有用,但我发现它并不能很好地用于基本调试。我最近一直在使用Devel :: Dwarn,这很容易安装。这就像一个警告的自卸车+整齐地卷起。
您的Catalyst应用程序将不会接收JSON。 dataType
自变量告诉$.ajax()
如何parse