为什么带有“_blank”的AJAX响应适用于第一个函数,而不适用于第二个函数?

时间:2022-09-10 18:27:28

I have a jsp where there are two radio buttons and based on the button clicked I call corresponding function. Both the functions have ajax call and the success response opens the new url in new tab. The first function does it perfectly , but second function doesn't work properly.

我有一个jsp,其中有两个单选按钮,并根据单击按钮我调用相应的功能。这两个函数都有ajax调用,成功响应在新选项卡中打开新的url。第一个功能完美,但第二个功能无法正常工作。

Console doesn't show anything. (Chrome)Pop up blocker blocks the response link from second function's ajax response, but not for the first function's ajax response.I don't want the (chrome) browser's pop up blocker to block it. This doesn't happen in Firefox and Safari. I haven't tried against IE yet. But right now happening only in Chrome.

控制台没有显示任何内容。 (Chrome)弹出窗口阻止程序阻止来自第二个函数的ajax响应的响应链接,但不阻止第一个函数的ajax响应。我不希望(chrome)浏览器的弹出窗口阻止程序阻止它。这在Firefox和Safari中不会发生。我还没有尝试过反对IE。但现在只发生在Chrome中。

function submit(){


    if($('input[name=questionOption]:checked').val() == "A"){   
        yesHelper();                                        
    }else if($('input[name=questionOption]:checked').val() == "B"){
        addHelper();
    }

}

function yesHelper(){
    var token = $("meta[name='_csrf']").attr("content");
    var header = $("meta[name='_csrf_header']").attr("content");

    var AQuestion = document.getElementById("A_Actual_Question_Id").value;
    var lang= "T";

    var stringifiedInput = JSON.stringify({"A_Actual_Question" : AQuestion, "language" : "T"}); 
    alert(stringifiedInput);
    $.ajax({
        url: "/Abcd/efgh/add1",
        type: "POST",           
        async: false,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        cache: false,
        data: stringifiedInput,
        beforeSend: function(xhr) {
            xhr.setRequestHeader(header, token);
        },
        success: function(response){
            window.open("http://localhost:8080/Abcd"+response, '_blank');
        }
    });         
}

function addHelper(){
    var token1 = $("meta[name='_csrf']").attr("content");
    var header1 = $("meta[name='_csrf_header']").attr("content");

    var B_Question = document.getElementById("B_ActualQuestion_Id").value;
    var lang1= "T";
    var B1 = document.getElementById("B1_Id").value;


    var stringifiedInput_B = JSON.stringify({"B_Question" : document.getElementById("B_ActualQuestion_Id").value,
    "language" : "T",
    "B1" : B1

    });

    $.ajax({
        url: "/Abcd/efgh/add2",
        type: "POST",           
        async: false,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        cache: false,
        data: stringifiedInput_B,
        beforeSend: function(xhr) {
            xhr.setRequestHeader(header1, token1);
        },
        success: function(response1){
             window.open("http://localhost:8080/Abcd"+response1, '_blank');

        }
    });         

}

1 个解决方案

#1


2  

If you don’t want browsers to block a popup, then you must open it directly on some sort of user interaction (such as a click on something) – otherwise, most current browsers will block it in default settings, because popups opened without user interaction are usually the ones users don’t want (advertising, or other annoying stuff.)

如果您不希望浏览器阻止弹出窗口,那么您必须直接在某种用户交互(例如点击某些内容)上打开它 - 否则,大多数当前浏览器将在默认设置中阻止它,因为弹出窗口在没有用户的情况下打开互动通常是用户不想要的(广告或其他讨厌的东西。)

Now, making an asynchronous AJAX request first, and then trying to open the popup in the success handler, has already “decoupled” this from the initial user interaction.

现在,首先制作异步AJAX请求,然后尝试在成功处理程序中打开弹出窗口,已经将其与初始用户交互“解耦”了。

You could instead try to:

你可以尝试:

  • make a synchronous AJAX request instead (not recommendable),

    改为制作同步AJAX请求(不推荐),

  • open the popup first (address about:blank), make the AJAX request, and then change the location of the popup window afterwards.

    首先打开弹出窗口(地址about:blank),发出AJAX请求,然后更改弹出窗口的位置。

#1


2  

If you don’t want browsers to block a popup, then you must open it directly on some sort of user interaction (such as a click on something) – otherwise, most current browsers will block it in default settings, because popups opened without user interaction are usually the ones users don’t want (advertising, or other annoying stuff.)

如果您不希望浏览器阻止弹出窗口,那么您必须直接在某种用户交互(例如点击某些内容)上打开它 - 否则,大多数当前浏览器将在默认设置中阻止它,因为弹出窗口在没有用户的情况下打开互动通常是用户不想要的(广告或其他讨厌的东西。)

Now, making an asynchronous AJAX request first, and then trying to open the popup in the success handler, has already “decoupled” this from the initial user interaction.

现在,首先制作异步AJAX请求,然后尝试在成功处理程序中打开弹出窗口,已经将其与初始用户交互“解耦”了。

You could instead try to:

你可以尝试:

  • make a synchronous AJAX request instead (not recommendable),

    改为制作同步AJAX请求(不推荐),

  • open the popup first (address about:blank), make the AJAX request, and then change the location of the popup window afterwards.

    首先打开弹出窗口(地址about:blank),发出AJAX请求,然后更改弹出窗口的位置。