通过方法FB.Event.subscribe订阅comment.create或comment.remove不会被触发 [英] Methods subscribed via FB.Event.subscribe to comment.create or comment.remove are not triggered
问题描述
我在我的MVC应用程序中的部分观点,我想显示一个Facebook的评论框。我也想保存的信息在我的数据库中,用户的帖子每个注释 - 是的,我知道他们在Facebook上是可见的,但是我需要将它们保存在数据库中。
I have a partial view in my MVC application where I want to display a Facebook comment box. I also want to save information in my database for each comment that a user posts - yes, I know they're visible on Facebook as well, but I need to save them in the database.
为此,我写了这个code(注:这是所有code我有我的部分观点,我试图把它分解为更具可读性此外,应用程序键是否正确插入。 ):
To that end, I wrote this code (NOTES: this is all the code I have in my partial view, I tried to split it to be more readable. Also, the app key is correctly inserted.):
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<dynamic>" %>
<div id="fb-root">
</div>
<script type="text/javascript">
window.fbAsyncInit = function ()
{
FB.init({ appId: 'MY_APP_KEY', status: true, cookie: true, xfbml: true }); console.log('fb init');
FB.Event.subscribe('comment.create',
function (response)
{
console.log("comment created");
var commentQuery = FB.Data.query("SELECT text, fromid FROM comment WHERE post_fbid='" + response.commentID +
"' AND object_id IN (SELECT comments_fbid FROM link_stat WHERE url='" + response.href + "')");
var userQuery = FB.Data.query("SELECT name FROM user WHERE uid in (select fromid from {0})", commentQuery);
FB.Data.waitOn([commentQuery, userQuery], function ()
{
var commentRow = commentQuery.value[0];
var userRow = userQuery.value[0];
console.log(userRow.name + " (id: " + commentRow.fromid + ") posted the comment: " + commentRow.text);
trackcomments(response['commentID'], response['href'], 'create', commentRow.text, userRow.name, commentRow.fromid);
});
});
FB.Event.subscribe('comment.remove',
function (response)
{
console.log("comment removed");
trackcomments(response['commentID'], response['href'], 'remove', null, null, null);
});
};
(function ()
{
var e = document.createElement('script');
e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js#xfbml=1';
e.async = true; document.getElementById('fb-root').appendChild(e);
} ()
);
该trackcomments功能:
The trackcomments function:
function trackcomments(_commentid, _address, _action,
_commentMessage, _userName, _userId)
{
$.ajax({
type: 'POST',
url: '/trackcomments',
data:
{
commentId: _commentid,
pageUrl: _address,
actionTaken: _action,
userName: _userName,
userId: _userId,
commentMessage: _commentMessage
},
success: function (result)
{
console.log("successfully received callback");
}
});
}
</script>
和facebook的注释标签,略低于上述code:
And the facebook comments tag, just below the above code:
<fb:comments notify="true" href="<%= Request.Url.ToString() %>" num_posts="3" width="630"></fb:comments>
的问题在于,每一个注释张贴(或删除)时,该功能不会触发。唯一的控制台日志的文字我看到的是FB INIT。显然,什么也没有发生在服务器上无论是。
The problem is that every time a comment is posted (or removed), the functions don't trigger. The only console log text I see is "fb init". Obviously, nothing happens on the server either.
我一直在抓我的头在过去两天,但我似乎无法找到答案 - 我会非常感激的人谁可以点我在正确的方向
I've been scratching my head for the last two days but I can't seem to find the answer - I'd be very grateful to anyone who could point me in the right direction.
感谢您!
推荐答案
您应该把相关的FB的一切。[...]在
You should put everything related to FB.[...] functions (FB.init, FB.Event.suscribe, ...) in
window.fbAsyncInit = function(){
// Your code
});
否则,你的code会被解析之前,FB JS SDK已经满负荷,你将永远不会真正suscribe您事件
Otherwise your code will be parsed before the FB js sdk has been fully loaded and you will never really suscribe to your event
这篇关于通过方法FB.Event.subscribe订阅comment.create或comment.remove不会被触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!