我正在开发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 %>
的application.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)
在谷歌浏览器中显示正确的JSONP响应
?({"user":{"fname":"Test","lname":"User","email":"testuser@testsite.com"}})
一切似乎都是正确的;但它仍然在错误块中给我提醒。为什么我没有获得成功警报?什么可能是错的? 任何帮助表示赞赏...
答案 0 :(得分: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
答案 1 :(得分: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