为什么用"_blank"响应AJAX?为第一个功能工作,但不为第二个功能工作? [英] Why does the response of AJAX with "_blank" work for first function, but not the second function?

查看:141
本文介绍了为什么用"_blank"响应AJAX?为第一个功能工作,但不为第二个功能工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个jsp,其中有两个单选按钮,根据单击的按钮,我调用了相应的函数.这两个函数都具有ajax调用,成功响应将在新选项卡中打开新的url.第一个功能可以完美地完成工作,但是第二个功能不能正常工作.

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.

控制台不显示任何内容. (Chrome)弹出式窗口拦截器会阻止来自第二个函数的Ajax响应的响应链接,但不会阻止第一个函数的ajax响应.我不希望(chrome)浏览器的弹出式窗口拦截器对其进行阻止.在Firefox和Safari中不会发生这种情况.我还没有尝试过针对IE.但是目前仅在Chrome中发生.

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.

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');

        }
    });         

}

推荐答案

如果您不希望浏览器阻止弹出窗口,则必须直接通过某种用户交互(例如单击某项)将其打开-否则,大多数当前的浏览器 会默认设置为阻止,因为在没有用户交互的情况下打开的弹出窗口通常是用户不想要的(广告或其他令人讨厌的东西).

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.)

现在,首先发出异步AJAX请求,然后尝试在成功处理程序中打开弹出窗口,已将其与初始用户交互分离".

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请求(不推荐),

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

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

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

这篇关于为什么用"_blank"响应AJAX?为第一个功能工作,但不为第二个功能工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆