4

在我的 Jquery绑定方法'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 %>      

阿贾克斯绑定

$('#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
4

1 回答 1

4

在 Jquery 中,Ajax Event 有两种类型。一个是本地事件,另一个是全局事件。

本地事件,它将使用 ajax 请求定义:

$.ajax('YOUR PATH', {success: function(){ alert(!) }});

全局事件,它应该与文档元素绑定:

$(document).bind('ajaxSuccess', function() {
    alert('!');
});

见 fiddlejquery api

于 2013-08-15T02:58:00.590 回答