Ruby on rails 3 ajax:远程提交表单时未调用succuess 3
基本上,我想提交一个createnewcourse表单,并使用JQuery处理响应。 我的身体像Ruby on rails 3 ajax:远程提交表单时未调用succuess 3,ruby-on-rails-3,jquery,Ruby On Rails 3,Jquery,基本上,我想提交一个createnewcourse表单,并使用JQuery处理响应。 我的身体像 <div id="create_coursem"> <%= form_tag({:controller => "coursem", :action => "create"}, :id => "check_coursem_form", :method => "post", :remote => true) do %> <p&g
<div id="create_coursem">
<%= form_tag({:controller => "coursem", :action => "create"}, :id => "check_coursem_form", :method => "post", :remote => true) do %>
<p>
<b> <%= label_tag(:name, "Name:") %> </b>
<%= text_field_tag(:name) %>
</p>
<p>
<b> <%= label_tag(:department, "Department:") %> </b>
<%= select_tag "department", @result.html_safe %>
</p>
<%= submit_tag("Create") %>
<% end %>
</div>
响应状态为200,这意味着它成功返回了json。在我的JS文件中,我有
$(document).ready(function() {
panelHandler();
//showOverview();
showResources();
resourceHandler();
calendarChangeMonth();
subscribeHandler();
shownewresourceform();
eventinfo();
closeeventbox();
showneweventform();
colorrecentevents();
check_coursem();
})
function check_coursem() {
$("form#check_coursem_form").on('ajax:beforeSend', function(xhr, settings) {alert("hello");})
.on('ajax:success', function(data, status, xhr) {alert("hello");})
.on('ajax:complete', function(xhr, status) {alert("hello");})
.on('ajax:error', function(xhr, status, error) {alert("hello");});
}
但是当我单击create按钮时,它只返回create控制器生成的json,没有显示任何警报。有人知道原因吗?
非常感谢。我还没有使用过ajax,但我从未见过这样的请求格式。。。 您是否尝试过在中实现更类似的功能
也许它试图将响应解析为javascript响应,而不是json响应 试试这个:
form_tag({:controller => "coursem", :action => "create"}, :id => "check_coursem_form", :method => "post", :remote => true, :data => { :type => 'json' }) do %>
// Assign handlers immediately after making the request,
// and remember the jqxhr object for this request
var jqxhr = $.ajax( "example.php" )
.done(function() { alert("success"); })
.fail(function() { alert("error"); })
.always(function() { alert("complete"); });
// perform other work here ...
// Set another completion function for the request above
jqxhr.always(function() { alert("second complete"); });
form_tag({:controller => "coursem", :action => "create"}, :id => "check_coursem_form", :method => "post", :remote => true, :data => { :type => 'json' }) do %>