我在我的MVC應用程序中有一個局部視圖,我想要顯示一個Facebook評論框。我還希望將數據庫中的信息保存在用戶發佈的每條評論中 - 是的,我知道它們也可以在Facebook上看到,但我需要將它們保存在數據庫中。爲了這個目的,我寫了這段代碼(注意:這是我的部分視圖中的所有代碼,我試圖將它拆分爲更具可讀性,並且應用程序密鑰已正確插入。):通過FB.Event.subscribe來訂閱comment.create或comment.remove的方法不會觸發
<%@ 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功能:
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的評論標籤,略低於上面的代碼:
<fb:comments notify="true" href="<%= Request.Url.ToString() %>" num_posts="3" width="630"></fb:comments>
問題是每次發佈評論(或刪除)時,這些功能都不會觸發。我看到的唯一控制檯日誌文本是「fb init」。顯然,服務器上也沒有任何反應。
過去兩天我一直在撓頭,但我似乎無法找到答案 - 我會非常感謝任何能指引我走向正確方向的人。
謝謝!
的FB代碼已經在window.fbAsyncInit塊... –
嗯我不好對不起,不知道是什麼問題那麼。 我有一個類似的問題,當2 FB.init哪裏觸發在同一頁面上,事件不發送到良好的實例,但我不認爲這是在這裏的原因... – Olivier