我有一个 ajax 请求和一个调用 Catalyst 控制器的测试。如何让参数显示在 Catalyst 对象中的同一位置?

发布于 2024-12-20 15:36:54 字数 1039 浏览 1 评论 0原文

测试片段:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烟─花易冷 2024-12-27 15:36:54

我不完全确定,但我怀疑 jQuery 会将您在 Ajax 请求中发送的数据附加到 GET 查询字符串。我的部分疑问是我不知道 .serializeObject() 在您的 Ajax 代码片段中做了什么。

您可以查看有关 dataprocessData 的文档 选项,看看是否有帮助。

以下是相关部分:

数据
要发送到服务器的数据。如果还不是字符串,它将转换为查询字符串。它附加到 GET 请求的 url 中。请参阅 processData 选项以阻止此自动处理。对象必须是键/值对。如果 value 是一个数组,jQuery 将根据传统设置的值(如下所述)序列化具有相同键的多个值。

...

处理数据
默认情况下,作为对象(从技术上讲,字符串以外的任何内容)传递到数据选项的数据将被处理并转换为查询字符串,适合默认内容类型“application/x-www-form-urlencoded” 。如果您想发送 DOMDocument 或其他未处理的数据,请将此选项设置为 false。

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 and processData options and see if that helps.

Here are the relevant sections:

data
Data to be sent to the server. It is converted to a query string, if not already a string. It's appended to the url for GET-requests. See processData option to prevent this automatic processing. Object must be Key/Value pairs. If value is an Array, jQuery serializes multiple values with same key based on the value of the traditional setting (described below).

...

processData
By default, data passed in to the data option as an object (technically, anything other than a string) will be processed and transformed into a query string, fitting to the default content-type "application/x-www-form-urlencoded". If you want to send a DOMDocument, or other non-processed data, set this option to false.

把人绕傻吧 2024-12-27 15:36:54

Catalyst 将输入存储在两个不同的位置。查询字符串参数在 中可用

$c->request->query_params

,正文参数在 中可用

$c->request->body_params

要获取所有输入,请使用

$c->request->params

从 Catalyst::Controller::REST 模块的文档中:

The HTTP POST, PUT, and OPTIONS methods will all automatically deserialize the
contents of $c->request->body into the $c->request->data hashref

Catalyst stores input in two different places. Query string parameters are made available in

$c->request->query_params

and body parameters are made available in

$c->request->body_params

To get all input, use

$c->request->params

From the documentation of the Catalyst::Controller::REST module:

The HTTP POST, PUT, and OPTIONS methods will all automatically deserialize the
contents of $c->request->body into the $c->request->data hashref
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文