1

我正在研究 Ruby-on-Rails。我正在尝试提交带有 JSON 请求的表单。

HTML

<%= form_tag widgets_user_path(:format => "json"), :id => "testjsonform" do %>
        <input type="checkbox" name="cat[]" value="math" id="sub1" class="left">
        <label for="sub1" class="laa">Math</label>
        <input type="checkbox" name="cat[]" value="history" id="sub2" class="left">
        <label for="sub2" class="laa">History</label>
        <input type="checkbox" name="cat[]" value="science" id="sub3" class="left">
        <label for="sub3" class="laa">Science</label>
        <%= hidden_field_tag 'callback', "?" %>
        <div><%= submit_tag 'JSON', :id => "testjson" %></div>
<% end %>

应用程序.js

$("#testjsonform").live("submit", function() {
    $.getJSON($(this).attr("action"),$(this).serialize(), function(data) {
        alert("success");
    })
    .success(function() {
        alert("success");
    })
    .error(function() {
        alert("error");
    });
    return false;
});

在控制台上显示请求已完成(200)

Started GET "/widgets/user.json?utf8=%E2%9C%93&authenticity_token=N8WsL3BATQiv9OWX4d29o62GOQgX9mXdt%2BBiVe%2FtzU%3D&cat%5B%5D=history&callback=%3F" for 127.0.0.1 at 2012-04-27 12:52:35 +0530
Processing by WidgetsController#user as JSON
Parameters: {"utf8"=>"✓", "authenticity_token"=>"N8WsL3BATQiv9OWX4d29o62GOQgX9mXdt+BiVe/tzU=", "cat"=>["history"], "callback"=>"?"}
User Load (0.3ms)  SELECT "users".* FROM "users" LIMIT 1
===================================== Categories
Completed 200 OK in 3ms (Views: 0.2ms | ActiveRecord: 0.3ms)

并且在 Google Chrome 中显示了正确的 JSONP 响应

?({"user":{"fname":"Test","lname":"User","email":"testuser@testsite.com"}})

一切似乎都是正确的;但它仍然让我在错误块中保持警惕。为什么我没有收到成功警报?什么可能是错的?任何帮助表示赞赏...

4

2 回答 2

1

好的...我找到了问题所在并提出了(不太漂亮)解决方案...

问题似乎与 URL 编码有关。(它发送“回调”参数的方式)

$.getJSON(_form.attr("action"), _form.serialize(), function(data) { /* some code */ }发送请求如下

Started GET "/widgets/user.json?utf8=%E2%9C%93&cat%5B%5D=4&callback=%3F&user=3" for 127.0.0.1 at 2012-04-27 16:59:40 +0530
Processing by WidgetsController#user as JSON
Parameters: {"utf8"=>"✓", "cat"=>["4"], "callback"=>"?", "user"=>"3"}
User Load (0.1ms)  SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", "3"]]
===================================== Categories
Completed 200 OK in 1ms (Views: 0.1ms | ActiveRecord: 0.1ms)

application.js我对参数进行了一些更改。

var _url = _form.attr("action") + "?" + decodeURIComponent(_form.serialize());

$.getJSON(_url, function(data) {
    alert("success");
});

这会按如下方式发送请求并给我成功警报:)(请参阅参数;额外的“_”参数也已添加到其中。)

Started GET "/widgets/user.json?utf8=%E2%9C%93&cat[]=4&callback=jQuery17203027302643749863_1335526106006&user=3&_=1335526108045" for 127.0.0.1 at 2012-04-27 16:58:28 +0530
Processing by WidgetsController#user as JSON
Parameters: {"utf8"=>"✓", "cat"=>["4"], "callback"=>"jQuery17203027302643749863_1335526106006", "user"=>"3", "_"=>"1335526108045"}
User Load (2.6ms)  SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", "3"]]
===================================== Categories
Completed 200 OK in 4ms (Views: 0.1ms | ActiveRecord: 2.6ms)

我也添加:method => "get"form

于 2012-04-27T11:47:21.340 回答
0

你为什么不把你的表格设置为remote?您可以使用 javascript 而不是 JSON 进行响应,后者可以在您的页面上显示一些呈现的内容:

# in your view
<%= form_tag widgets_user_path, :id => "testjsonform", remote: true do %>

# in *.js.erb
alert("<%= j params.to_yaml %>")
$('body').append("<%= j render something %>") # or other stuff for filling your "parent" page
于 2012-04-27T08:35:10.007 回答