gpt4 book ai didi

jquery - 在 jQuery ajax 成功回调中创建时弹出窗口被阻止

转载 作者:行者123 更新时间:2023-12-03 22:12:00 25 4
gpt4 key购买 nike

Google Chrome 似乎阻止了我通过 jQuery 创建的弹出窗口。经过一番调查,这似乎是在 ajax 调用成功事件中调用 window.open 的问题。有没有解决的办法?我的 jQuery ajax 调用返回要打开的 URL。所以我有点卡住了。

如果我将 window.open 放在 ajax 调用之外,它就会起作用;但是,在内部(即在成功事件中)它被阻止。我认为这与上下文有关,但我不确定。

这是我所拥有的:

     window.open("https://www.myurl.com");  // OUTSIDE OF AJAX - no problems 
// with popup

$.ajax({
type: "POST",
url: "MyService.aspx/ConstructUrl",
data: jsonData,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
// Normally loads msg.d which is the url returned from service
// static url below is for testing
window.open("https://www.myurl.com"); // THIS IS BLOCKED
},
error: function(msg) {
// alert(error);
}
});

最佳答案

正如一些人指出的那样,接受的答案不再有效。根据 aidiakapi 的评论,我使用了一种解决方法,首先打开窗口。

window.open("about:blank", "myNewPage");

然后执行ajax业务,并在done()函数中,打开具有该名称的页面。

window.open("/foo.html", "myNewPage");

无论您是否异步执行,这也无关紧要。

关于jquery - 在 jQuery ajax 成功回调中创建时弹出窗口被阻止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1086672/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com