在我的Jquery bind 方法中,'ajax:success', 'ajax:error' or 'ajax:complete'
回调未被执行。表单是通过Ajax提交的,并且数据已成功插入到数据库中,但这些回调函数根本没有被调用(它只是应该在这些回调中弹出警告。)我使用的是Jquery 1.6,Rails 3.1这里是我的代码。提前谢谢你。
表格
<%= form_for(resource,:as=>resource_name,:url => registration_path(resource_name), :html => {:id => "sign_up_user"},:remote => true,:format => :json) do |f| %>
<%= devise_error_messages! %>
<%= f.label :Username %>
<%= f.text_field :username %></p>
<%= f.label :email %>
<%= f.text_field :email %>
<%= f.label :password %>
<%= f.password_field :password %>
<%= f.label :password_confirmation %>
<%= f.password_field :password_confirmation %>
<%= f.submit "Sign up" ,:id=>"sign-up" %>
<% end %>
Ajax绑定
$('#sign_up_user').bind('ajax:success', function( data, status, xhr) {
alert("success");
});
控制器
class RegistrationsController < Devise::RegistrationsController
def create
build_resource
if resource.save
if resource.active_for_authentication?
set_flash_message :notice, :signed_up if is_navigational_format?
sign_up(resource_name, resource)
return render :json => {:success => true}
else
set_flash_message :notice, :"signed_up_but_#{resource.inactive_message}" if is_navigational_format?
expire_session_data_after_sign_in!
return render :json => {:success => true}
end
else
clean_up_passwords resource
return render :json => {:success => false}
end
end
# Signs in a user on sign up. You can overwrite this method in your own RegistrationsController
def sign_up(resource_name, resource)
sign_in(resource_name, resource)
end
end
日志
Started POST "/users" for 127.0.0.1 at 2013-08-13 22:12:51 -0500
Processing by Devise::RegistrationsController#create as JS
Parameters: {"utf8"=>"✓", "authenticity_token"=>"mrcZMq5WT1QPNpGDGsVFWIPx+WqfI0PZmHfGqs7jnrM=", "user"=> {"username"=>"ksks", "email"=>"jsjs@t.comq", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "address"=>"", "business"=>"0"}, "commit"=>"Sign up"}
(0.2ms) SELECT 1 FROM "users" WHERE LOWER("users"."email") = LOWER('jsjs@t.comq') LIMIT 1
SQL (2.6ms) INSERT INTO "users" ("address", "business", "business_name", "business_type", "city", "created_at", "current_sign_in_at", "current_sign_in_ip", "email", "encrypted_password", "last_sign_in_at", "last_sign_in_ip", "phone", "remember_created_at", "remember_token", "reset_password_token", "sign_in_count", "state", "updated_at", "username", "zipcode") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) [["address", nil], ["business", false], ["business_name", nil], ["business_type", nil], ["city", nil], ["created_at", Wed, 14 Aug 2013 03:12:52 UTC +00:00], ["current_sign_in_at", nil], ["current_sign_in_ip", nil], ["email", "jsjs@t.comq"], ["encrypted_password", "$2a$10$uynKQ8s3sjBe5W3LfqFxSO9Z2jg9FB.m8LbVOiiBNqDtT9qF1j7Sy"], ["last_sign_in_at", nil], ["last_sign_in_ip", nil], ["phone", nil], ["remember_created_at", nil], ["remember_token", nil], ["reset_password_token", nil], ["sign_in_count", 0], ["state", nil], ["updated_at", Wed, 14 Aug 2013 03:12:52 UTC +00:00], ["username", "ksks"], ["zipcode", nil]]
(0.4ms) UPDATE "users" SET "last_sign_in_at" = '2013-08-14 03:12:52.570849', "current_sign_in_at" = '2013-08-14 03:12:52.570849', "last_sign_in_ip" = '127.0.0.1', "current_sign_in_ip" = '127.0.0.1', "sign_in_count" = 1, "updated_at" = '2013-08-14 03:12:52.572092' WHERE "users"."id" = 35
答案 0 :(得分:4)
在Jquery中,Ajax Event有两种类型。一个是本地事件,另一个是全球事件。
本地事件,将使用ajax请求定义:
$.ajax('YOUR PATH', {success: function(){ alert(!) }});
全球事件,应该与文档元素绑定:
$(document).bind('ajaxSuccess', function() {
alert('!');
});