我有几个邀请这样的按钮的观点:
< DIV CLASS =fbinvite_formID =<%=的朋友[:符]%>中NAME =FB>
< DIV CLASS =BTN BTN小>
邀请
< / DIV>
< / DIV>
当其中一个按钮被点击一个AJAX调用函数(invite_friend):
$('。fbinvite_form)。点击(函数(){
invite_friend();
});
下面是invite_friend(有些值是硬codeD为我调试):
函数invite_friend(){
.post的$('/组/ 7 /请柬,
{
RECIPIENT_EMAIL:facebook@meetcody.com,
承诺:FB,
FB:FB
},
功能(响应){
});
}
下面是从控制器返回的相关线路:
渲染:JSON => {
:URL => signup_with_token_url(@ invitation.token)
:ID => @ invitation.id
},
:状态=> :创建
我可以证实,这JSON是得到正常显示。在这一点上,我期待一个 AJAX:成功
事件激发。我有以下的code在我的页面的顶部:
$('fbinvite_form。')绑定(AJAX:成功,功能(EVT,数据,身份,XHR){
...
});
但它不是射击。任何线索什么可能会出错或如何更好地解决(我是一个有点小白的)?
其他上下文
我想补充一点点,因为它可能会有所帮助。我原本建立了这个用表格来工作,它工作得很好。对于一些性能方面的考虑,我决定用AJAX切换到按钮。这里的原始形式:
<%=的form_for([@组,@invitation]:远程=>真,:HTML => {:'数据类型'=>'HTML ',:类=>fbinvite_form',:ID =>朋友[:标识符]})做| F | %>
<%= f.hidden_field:RECIPIENT_EMAIL,:价值=> facebook@meetcody.com%>
< DIV CLASS =fbinvite btn_list_rightID =<%=的朋友[:符]%>>
<%= f.submit邀请,:类=> BTN BTN-中BTN-小学,:名称=> FB%>
< / DIV>
<%结束%GT;
现在已经被替换了所有code,你看上面的控制器代码段。
更新1
每文斯的建议,我提出了阿贾克斯:成功code到成功的功能。原来这里是阿贾克斯:成功功能:
$('fbinvite_form。')绑定(AJAX:成功,功能(EVT,数据,身份,XHR){
VAR fb_id = $(本).attr('身份证');
变种响应=的eval((+ xhr.responseText +));
VAR link_url = response.url;
VAR ID = response.id;
VAR inv_url =<%=原(''+ group_url(@group)+'/请柬/')%> + ID;
VAR picture_url =https://www.example.com.com/assets/cody_130by130.png;
VAR DESC =<%=原(''+ FIRST_NAME(CURRENT_USER)+正在与科迪健身加入+ genderizer(CURRENT_USER,他)+集团开始跟踪你的训练Cody和另一个。小组成员将让你的动机!+')%取代;
send_invite(fb_id,link_url,picture_url,递减,inv_url);返回false;
});
这里是我做了什么,移动code到成功的功能。问题是,我似乎并没有获得XHR
$。阿贾克斯({
键入:POST,
网址:/组/ 7 /请柬,
数据:{RECIPIENT_EMAIL:facebook@meetcody.com,承诺:FB,FBFB},
数据类型:JSON,
成功:函数(EVT,数据,身份,XHR){
VAR fb_id = $(本).attr('身份证');
变种响应=的eval((+ xhr.responseText +));
VAR link_url = response.url;
VAR ID = response.id;
VAR inv_url =<%=原(''+ group_url(@group)+'/请柬/')%> + ID;
VAR picture_url =https://www.meetcody.com/assets/cody_130by130.png;
VAR DESC =<%=原(''+ FIRST_NAME(CURRENT_USER)+正在与科迪健身加入+ genderizer(CURRENT_USER,他)+集团开始跟踪你的训练Cody和另一个。小组成员将让你的动机!+')%取代;
send_invite(fb_id,link_url,picture_url,递减,inv_url);返回false;
}
});
解决方案
添加的错误处理类似这样并记录错误,这将有助于诊断问题。
错误:函数(XHR,状态,错误){
执行console.log(错误);
}
修改
对不起,您需要使用代替。员额阿贾克斯。
$。阿贾克斯({
键入:POST,
网址:/组/ 7 /请柬,
数据:名称=约翰和放大器;位置=波士顿,
成功:函数(MSG){
警报(数据保存:+味精);
},
错误(XHR,状态,错误){
执行console.log(错误);
}
});
I have a view with several "Invite" buttons like this:
<div class = "fbinvite_form" id = "<%= friend[:identifier] %>" name = "fb">
<div class = "btn btn-small">
Invite
</div>
</div>
When one of these buttons are clicked an AJAX function is called (invite_friend) :
$('.fbinvite_form').click(function() {
invite_friend();
});
Here's invite_friend (some values are hard-coded as I debug):
function invite_friend() {
$.post('/groups/7/invitations',
{
"recipient_email": "facebook@meetcody.com",
"commit" : "fb",
"fb" : "fb"
},
function(response) {
});
}
Here's the relevant line that is returned from the controller:
render :json => {
:url => signup_with_token_url(@invitation.token),
:id => @invitation.id
},
:status => :created
I can confirm that this json is being rendered correctly. At this point I'm expecting an ajax:success
event to fire. I have the following code at the top of my page:
$('.fbinvite_form').bind("ajax:success", function(evt, data, status, xhr) {
...
});
But it's not firing. Any clue what might be going wrong or how to better troubleshoot (I'm a bit of a noob)?
Additional Context
I wanted to add a little bit more as it might help. I had originally built this to work with a form and it worked fine. For some performance reasons I decided to switch to buttons with AJAX. Here's the original form:
<%= 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 %>
This has since been replace with all the code you see above the controller snippet.
UPDATE 1
Per Vince's suggestion I have moved the "ajax:success" code into the success function. Here is the original "ajax:success" function:
$('.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;
});
And here is what I've done to move the code into the success function. The issue is that I don't seem to have access to "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;
}
});
解决方案
Add error handler like this and log the error, this should help diagnose the issue.
error: function(xhr, status, error) {
console.log(error);
}
EDIT
Sorry you need to use .ajax instead of .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);
}
});