1

I am working on this seemingly trivial problem since three days and ran completely out of ideas why my code doesn't work.

In a nutshell, when the user receives a facebook request and clicks on it, it should be process the invitation.

FB.Event.subscribe('auth.authResponseChange', function (response) {
            if (response.status === 'connected') {
                if (window.location.href.indexOf('app_invite') !== -1 || window.location.href.indexOf('app_request') !== -1) {
                    var inviteeID = response.authResponse.userID;                                    
                    processIncomingInvitation(inviteeID);                    
                }
});

The problem occurs in the following function. Upon the successful $.post() request I am expecting a simple redirect:

$.post(url, function (result) {
   window.location.replace('/True?fbapp=fbapp');
}); 

But the redirect is ignored and I don't understand why. I even put an alert('hello'); in there instead and I can clearly see it is hitting that bit of code. Why is the redirect ignored instead?

function processIncomingInvitation(inviteeID) {
    var urlParams = {};
    (function () {
        var match,
        pl     = /\+/g,  // Regex for replacing addition symbol with a space
        search = /([^&=]+)=?([^&]*)/g,
        decode = function (s) { 
            return decodeURIComponent(s.replace(pl, " ")); 
        },
        query  = window.location.search.substring(1);

        while (match = search.exec(query)) {
            urlParams[decode(match[1])] = decode(match[2]);
        }
    })();

    var requestType = urlParams.app_request_type;

    if (requestType === "user_to_user") {
        var reqIDlist = urlParams.request_ids.split(',');
        var requestID = reqIDlist[0];

        FB.api(requestID, function (response) {  
            if (response.from !== undefined && response.from !== 'undefined') {
                var inviterID = response.from.id;
                var inviterName = response.from.name.split(" ")[0];
                var url = '/friend/' + inviteeID + '/accept/' + inviterID + '/?fbapp=fbapp';                            
                $.post(url, function (result) {
                    window.location.replace('/True?fbapp=fbapp');
                }); 
                deleteRequest(requestID);
            }
        });
    }
}
4

1 回答 1

0

我终于找到了问题所在。当您登录 FB 时,重定向被另一个重定向覆盖。

FB.Event.subscribe('auth.login', function (response) {
            if (window.location.href.indexOf('notif_t=app_request') !== -1) {
                alert('app_request: ' + window.location.href);
            } else if (window.location.href.indexOf('notif_t=app_invite') !== -1) {
                alert('app_invite: ' + window.location.href);               
            } else if (window.location.href.indexOf('fbapp') !== -1) {
                alert('fbapp: ' + window.location.href);
                window.location.replace('/?fbapp=fbapp');
            }            
        });

您应该在处理邀请后重定向,FB.Event.subscribe('auth.authResponseChange', function (response) { ... }如问题中所述。

因此,在用户登录时,您不应重定向,否则两个重定向将发生冲突。invite但是如果它不是or ,你仍然想重定向request。否则,返回用户会卡在登录页面上。

上面的代码,现在正是这样做的。我希望这有帮助。

于 2013-08-05T15:14:52.973 回答