我有一个 ajax 请求和一个调用 Catalyst 控制器的测试。如何让参数显示在 Catalyst 对象中的同一位置?
测试片段:
my $request = HTTP::Request->new( POST => 'http://192.168.5.130:3000/user' );
$request->content_type('application/json');
$request->content( $query_string );
my $result = $ua->request( $request );
来自 ajax 调用的片段:
$.ajax({
url: 'http://192.168.5.130:3000/user',
type: 'POST',
cache: false,
async: true,
dataType: 'json',
timeout: 5000,
data: $("#create-user-form").serializeObject(),
error: function(jqXHR, textStatus, errorThrown){
console.log("jQuery ajax error....");
},
);
在控制器中是路由处理程序(使用 Catalyst::Controller::Rest
)
sub user_POST {
my ($self, $c, $args) = @_;
warn Dumper( $c );
...
}
我遇到的问题是,当我从测试内容是 在哈希中的 Catalyst 对象中设置一个名为“data”的键,该键可以是 通过调用$c->req->data
来访问。
但是,当从网页进行ajax调用时,数据是 保存在 Catalyst 对象中的哈希中,其键名为“parameters”,即 通过调用$c->req->parameters
来访问。
任何人都知道为什么会发生这种情况以及或如何解决它?
Snippet from the test:
my $request = HTTP::Request->new( POST => 'http://192.168.5.130:3000/user' );
$request->content_type('application/json');
$request->content( $query_string );
my $result = $ua->request( $request );
Snippet from the ajax call:
$.ajax({
url: 'http://192.168.5.130:3000/user',
type: 'POST',
cache: false,
async: true,
dataType: 'json',
timeout: 5000,
data: $("#create-user-form").serializeObject(),
error: function(jqXHR, textStatus, errorThrown){
console.log("jQuery ajax error....");
},
);
In the controller is the route handler (using Catalyst::Controller::Rest
)
sub user_POST {
my ($self, $c, $args) = @_;
warn Dumper( $c );
...
}
The problem I'm having is, when I make the call from the test the content is
set in the Catalyst object in a hash with a key called 'data' which can be
accessed be calling $c->req->data
.
However, when the ajax call is made from a webpage, the data is
held in the Catalyst object in a hash with a key called 'parameters' which is
accessed by calling $c->req->parameters
.
Anyone know why this is happening and or what to do to work around it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不完全确定,但我怀疑 jQuery 会将您在 Ajax 请求中发送的数据附加到 GET 查询字符串。我的部分疑问是我不知道
.serializeObject()
在您的 Ajax 代码片段中做了什么。您可以查看有关
data
和processData 的文档
选项,看看是否有帮助。以下是相关部分:
I'm not completely sure, but I would suspect that jQuery is appending the data you are sending in the Ajax request to the GET query string. Part of my doubt is that I do not know what
.serializeObject()
does in your Ajax snippet.You might check the documentation regarding the
data
andprocessData
options and see if that helps.Here are the relevant sections:
Catalyst 将输入存储在两个不同的位置。查询字符串参数在 中可用
,正文参数在 中可用
要获取所有输入,请使用
从 Catalyst::Controller::REST 模块的文档中:
Catalyst stores input in two different places. Query string parameters are made available in
and body parameters are made available in
To get all input, use
From the documentation of the Catalyst::Controller::REST module: