我想使用 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')