为什么这个“ajax:success”事件没有被解雇?

时间:2012-07-26 06:47:20

标签: ruby-on-rails ajax ruby-on-rails-3 jquery

我有一个带有几个“邀请”按钮的视图,如下所示:

<div class = "fbinvite_form" id = "<%= friend[:identifier] %>" name = "fb">
    <div class = "btn btn-small">
        Invite
    </div>
</div>

当单击其中一个按钮时,将调用AJAX函数(invite_friend):

$('.fbinvite_form').click(function() {
    invite_friend();
});

这是invite_friend(有些值在我调试时是硬编码的):

function invite_friend() {      
    $.post('/groups/7/invitations',
        {
            "recipient_email": "facebook@meetcody.com",
            "commit" : "fb",
            "fb" : "fb"
        },
        function(response) {
        });
}

以下是从控制器返回的相关行:

render :json => {
    :url => signup_with_token_url(@invitation.token),
    :id => @invitation.id
},
:status => :created

我可以确认这个json正在正确呈现。此时我预计会发生ajax:success事件。我的页面顶部有以下代码:

$('.fbinvite_form').bind("ajax:success", function(evt, data, status, xhr) {
    ...
});

但它没有开火。任何可能出错的线索或如何更好地排除故障(我有点像菜鸟)?

附加背景

我想补充一点,因为它可能有所帮助。我最初构建它是为了使用表单,它工作正常。出于某些性能原因,我决定使用AJAX切换到按钮。这是原始形式:

<%= form_for([@group, @invitation], :remote => true, :html => { :'data-type' => 'html', :class => 'fbinvite_form', :id => friend[:identifier]}) do |f| %>
    <%= f.hidden_field :recipient_email, :value => "facebook@meetcody.com" %>

    <div class = "fbinvite btn_list_right" id = "<%= friend[:identifier] %>">
    <%= f.submit "Invite", :class => "btn btn-medium btn-primary", :name => "fb" %>
    </div>
<% end %>

此后,您已在控制器代码段上方看到所有代码。

更新1

Per Vince的建议我已将“ajax:success”代码移入成功函数中。这是最初的“ajax:success”功能:

      $('.fbinvite_form').bind("ajax:success", function(evt, data, status, xhr){
          var fb_id = $(this).attr('id');
          var response = eval("(" + xhr.responseText + ")");        
          var link_url = response.url;
          var id = response.id;
          var inv_url = <%= raw('"' + group_url(@group) + '/invitations/"') %> + id;
          var picture_url = "https://www.example.com.com/assets/cody_130by130.png";
          var desc = <%= raw('"' + first_name(current_user) + " is working with Cody on fitness. Join " + genderizer(current_user, "his") + " group to start tracking your workouts. Cody and the other group members will keep you motivated!" + '"') %>;
          send_invite(fb_id, link_url, picture_url, desc, inv_url); return false;
        });

以下是我将代码移入成功函数所做的工作。问题是我似乎无法访问“xhr”

    $.ajax({
        type: "POST",
        url: "/groups/7/invitations",
        data: {recipient_email: "facebook@meetcody.com", commit : "fb", fb : "fb" },
        dataType: "json",
        success: function(evt, data, status, xhr) {
              var fb_id = $(this).attr('id');
              var response = eval("(" + xhr.responseText + ")");        
              var link_url = response.url;
              var id = response.id;
              var inv_url = <%= raw('"' + group_url(@group) + '/invitations/"') %> + id;
              var picture_url = "https://www.meetcody.com/assets/cody_130by130.png";
              var desc = <%= raw('"' + first_name(current_user) + " is working with Cody on fitness. Join " + genderizer(current_user, "his") + " group to start tracking your workouts. Cody and the other group members will keep you motivated!" + '"') %>;
              send_invite(fb_id, link_url, picture_url, desc, inv_url); return false;
        }
        });

1 个答案:

答案 0 :(得分:1)

像这样添加错误处理程序并记录错误,这应该有助于诊断问题。

        error: function(xhr, status, error) {
              console.log(error);

        }

修改

很抱歉,你需要使用.ajax而不是.post。

$.ajax({
  type: "POST",
  url: "/groups/7/invitations",
  data: "name=John&location=Boston",
  success: function(msg){
        alert( "Data Saved: " + msg );
  },
  error(xhr, status, error) {
     console.log(error);
  }
});