开发者

Multiple request when FB edge.create Event catched

开发者 https://www.devze.com 2023-01-29 03:20 出处:网络
I wanted to use facebook\'s like bu开发者_JAVA百科tton 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 th

I wanted to use facebook's like bu开发者_JAVA百科tton 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')


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.

FB.Event.subscribe('edge.create', function(response) { 
            if ( response.toString() == shareUrl.toString() ) {} }


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:

<script src="http://connect.facebook.net/en_US/all.js"></script>
0

精彩评论

暂无评论...
验证码 换一张
取 消