2

我有几个像这样的“邀请”按钮的视图:

<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();
});

这是邀请朋友(一些值在我调试时是硬编码的):

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

根据文斯的建议,我已将“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;
        }
        });
4

1 回答 1

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);
  }
});
于 2012-07-26T08:29:02.740 回答