FB edge.create 事件捕获时的多个请求
我想使用 Facebook 的点赞按钮在我的页面上投票。不幸的是,当单击“赞”时,我的函数会收到 3-5 个请求,而不是只有一个。有没有办法防止这种情况?示例代码:
FB.Event.subscribe('edge.create', function(href, widget) {
console.log(href, widget);
});
我的代码:
FB.Event.subscribe('edge.create', function(href, widget) {
$.ajax({
type: "POST",
url: "/votes/register",
data: "href="+href,
dataType: 'json',
success: function(data){
$(".list-submissions").html(data["html"])
}
});
return false;
});
或者我可以使用 django 从函数端阻止它?这是我的功能代码:
def register_vote(request):
ip = request.META['REMOTE_ADDR']
url = request.POST.get("href", "")
id = os.path.basename(url)
try:
vote = Vote.objects.filter(ip=ip, id=id)
except:
vote = None
if not vote:
vote = Vote(ip=ip, uid=id)
vote.save()
html = render_finalists(request)
ajax = simplejson.dumps({
"html": html
}, cls=LazyEncoder)
return HttpResponse(ajax, mimetype='application/javascript')
I wanted to use facebook's like button for voting on my page. Unfortunately when 'Like' is clicked I get 3-5 requests to my function instead of only one. Is there a way to prevent this ?Sample code:
FB.Event.subscribe('edge.create', function(href, widget) {
console.log(href, widget);
});
My code:
FB.Event.subscribe('edge.create', function(href, widget) {
$.ajax({
type: "POST",
url: "/votes/register",
data: "href="+href,
dataType: 'json',
success: function(data){
$(".list-submissions").html(data["html"])
}
});
return false;
});
Or maybe I can block this from the function's side using django ? Here's my function code:
def register_vote(request):
ip = request.META['REMOTE_ADDR']
url = request.POST.get("href", "")
id = os.path.basename(url)
try:
vote = Vote.objects.filter(ip=ip, id=id)
except:
vote = None
if not vote:
vote = Vote(ip=ip, uid=id)
vote.save()
html = render_finalists(request)
ajax = simplejson.dumps({
"html": html
}, cls=LazyEncoder)
return HttpResponse(ajax, mimetype='application/javascript')
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我确实面临着稍微相同的问题,一直在使用 AJAX 动态创建类似的按钮,特定于内容,但“edge.create”一些如何存储事件并增加 Edge.create 事件并在我单击另一个 FB 时多次触发就像小部件一样。
蜜蜂严重撞到了我的头,还没有运气:(
任何快速帮助,都应该感激。
最后我破解了它,只是将我从响应对象得到的响应与我需要传递的响应进行了比较,并且有效。
I do facing slightly same issue, have been creating like buttons on the fly using AJAX, specific to the content but 'edge.create' some how storing the event and incrementing the edge.create events and firing multiple times when I click on another FB like widget.
Have bee hitting my head so badly, no luck till yet :(
Any quick help, should be appreciated.
Finally I cracked it, just compared the response which I was getting from the response object to the one which I needed to pass and that works.
如果多次引用 Facebook 核心 JS SDK,即多次引用以下脚本标记(或变体),您可能会收到对回调函数的多个请求:
You might get multiple requests to you callback function if the Facebook core JS SDK has been referenced multiple times, i.e. the following script tag (or variations) are more than once: