如何关闭弹出窗口并使用jQuery重定向

时间:2014-04-15 12:51:55

标签: jquery ajax redirect popup

我有一个弹出窗口和一个应该在ajax调用和服务器端进程之后发生的重定向。

除非框架未关闭且页面未重定向。

这是脚本:

jQuery.support.cors = true; // needed for ajax to work in certain older browsers and versions

$(document).ready(function(){

    $('input[name="status"]').on("change", function() {

        if ($('input:radio[name="status"]:checked').val() == 'Y') {

            $.ajax({
                type: "GET",
                url: "http://mydomain.com/ajax/serverScript.php",
                data: "action=" + $('#action').val() + "&id=" + ( $('#id').val() * 1 ) + "&mode=" + $('#mode').val()
            }); // end .ajax()

            alert('server process finished'); // ajax call will not work with out this here -- needs further research and understanding
            //window.parent.closePP(); // tried this here but does not work
            //window.top.location.href = $('#redirect').val();  // tried this here to reload page but does not work

        }   // end if()

        window.parent.closePP(); // this does not work
        window.top.location.href = $('#redirect').val();    // reloads page but does not work

    }); // end .on("change")

}); // end .ready()

根据@ZiNNED的答案附加

我的服务器端PHP脚本运行正常。但它本身就是一个独立的脚本。我是否需要将某些内容返回给ajax .JS调用函数才能完成交互?

这是[object Object]的转储,如果它对任何人都有意义 enter image description here

以下是我从console.log(e)

获得的内容
XMLHttpRequest cannot load http://mydomain.com/ajax/serverScript.php?action=insert&id=1&mode=_test. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://mydomain.com' is therefore not allowed access. redirectPage.html:1
Object {readyState: 0, setRequestHeader: function, getAllResponseHeaders: function, getResponseHeader: function, overrideMimeType: function…}

1 个答案:

答案 0 :(得分:2)

尝试这样的事情?使用ajax的successerrorcomplete函数?

$(document).ready(function(){
    $('input[name="status"]').on("change", function() {
        if ($('input:radio[name="status"]:checked').val() == 'Y') 
        {
            $.ajax({
                type: "GET",
                url: "http://mydomain.com/ajax/serverScript.php",
                data: "action=" + $('#action').val() + "&id=" + ( $('#id').val() * 1 ) + "&mode=" + $('#mode').val(),
                success: function(d)
                {
                    // do something with the result d?
                    window.parent.closePP();
                    window.top.location.href = $('#redirect').val();
                },
                error: function()
                {
                    // do something when the call goes wrong?
                },
                complete: function()
                {
                    // do something when the call finishes, either successful or not?
                }
            }); 
        } 
    }); 
});