noclist
noclist

Reputation: 1819

ajax call behaving differently depending on browser

I have the following function that executes when a user logs out. It contains an ajax call with success and failure functions within it. My site uses a chat widget from zendesk who provide an API to perform various actions on the chat window. On successful logout, I am executing an endChat() function to end the current chat. In Chrome, this works great and I can debug with the developer tools and watch the execution pointer hit $zopim.livechat.endChat(); However, in both FireFox and IE the user is logged out before that line is ever hit. Is there a discrepancy between the way these browsers handle ajax calls?

function logout(supressConfirmation, reason) {
    var url = $("#LogoutStatus").data('url');
    $.ajax({
        url: url,
        type: "Get",
        data: {},
        success: function (data) {
            if (data.isUpdating && supressConfirmation != true) {
                $("#logout-modal").modal('show');
            }
            else {
                window.location.href = '@Html.Raw(Url.Action("Logout","Logout"))' + '?reason=' + reason;
            }
            zE(function () {
                $zopim(function () {
                    $zopim.livechat.endChat();
                });
            });
        },
        error: function (failure) {
        }

    });
}

Upvotes: 0

Views: 109

Answers (1)

Hackinet
Hackinet

Reputation: 3328

Use async:false so that it waits until Ajax call is returned and only then moves on the next line that logs you out.

So your modified version is:

    url: url,
    type: "Get",
    data: {},
    async:false,
    success: ...

Upvotes: 1

Related Questions