我正在研究 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"}})
一切似乎都是正确的;但它仍然让我在错误块中保持警惕。为什么我没有收到成功警报?什么可能是错的?任何帮助表示赞赏...