重定向不工作之前的ajax

时间:2015-10-19 19:55:27

标签: javascript php jquery ajax redirect

因此,点击链接,我想对脚本进行ajax调用,但在某些情况下,我还想重定向页面。当条件不满足且页面没有重定向时,脚本运行正常(它几乎只设置一个cookie),但我怀疑当页面重定向时会发生什么,ajax没有机会点击脚本在它重定向之前。

我无法承担在ajax成功回调中的重定向,因为这是该网站的一个非常常见的功能,它将大大减慢用户体验。

真诚地感谢您的帮助。非常感谢。

$("li").click(function(e) {

        var addressValue = $(this).find('a:first').attr("href");
        var hash_tag = $(this).find('a:first');
        var iframe_header = hash_tag.text();
        var hashValue = hash_tag.attr("id");
        var dest = '/site_client/' + '#' + hashValue;
        alert(dest);
        alert(iframe_header);
        $.ajax({
            url: "/site_client/set_redirect_back/",
            data: {redirect_back: dest, redirect_back_page_name: iframe_header},
            dataType: "json",
            type: "POST"

        });
        if(window.location.href.indexOf("site_client") === -1) {

            window.location.href = dest;
            e.preventDefault();

         }else{

            $('#iframe1').hide();
            var spinner = $('#loading-spinner');
            $(spinner).show();

            $('#tool-header').text(iframe_header);

            location.hash = hashValue;
            document.getElementById('iframe1').src = addressValue;
            e.preventDefault();

        }

    });

1 个答案:

答案 0 :(得分:0)

成功回调调用JavaScript:



$("li").click(function(e) {
    var addressValue = $(this).find('a:first').attr("href");
    var hash_tag = $(this).find('a:first');
    var iframe_header = hash_tag.text();
    var hashValue = hash_tag.attr("id");
    var dest = '/site_client/' + '#' + hashValue;
    alert(dest);
    alert(iframe_header);
    $.ajax({
        url: "/site_client/set_redirect_back/",
        data: {redirect_back: dest, redirect_back_page_name: iframe_header},
        dataType: "json",
        type: "POST",
        success: function () {
            if(window.location.href.indexOf("site_client") === -1) {

                window.location.href = dest;
                e.preventDefault();

             }else{

                $('#iframe1').hide();
                var spinner = $('#loading-spinner');
                $(spinner).show();

                $('#tool-header').text(iframe_header);

                location.hash = hashValue;
                document.getElementById('iframe1').src = addressValue;
                e.preventDefault();

            }
        }
    });
});