Popup blocked when created in jQuery ajax success callback(在 jQuery ajax 成功回调中创建时弹出窗口被阻止)
问题描述
Google Chrome 似乎阻止了我通过 jQuery 创建的弹出窗口.经过一番调查,在 ajax 调用的成功事件中调用 window.open
似乎是一个问题.有没有解决的办法?我的 jQuery ajax 调用返回要打开的 URL.所以我有点卡住了.
Google Chrome seems to be blocking a popup I am creating via jQuery. After some investigation, it appears to be an issue with calling window.open
in the success event of an ajax call. Is there a way around this? My jQuery ajax call returns the URL to be opened. So I am bit stuck.
如果我将 window.open
放在 ajax 调用之外,它会起作用;但是,在内部(即在成功事件中)它被阻止了.我认为这与 CONTEXT 有关,但我不确定.
It works if I place the window.open
outside the ajax call; but, inside (i.e. in the success event) it is blocked. I think it is something to do with CONTEXT but I am unsure.
这是我所拥有的:
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);
}
});
推荐答案
只需在成功回调中打开新窗口即可:
Simply open the new window in the success callback:
$.ajax({
type: "POST",
url: "MyService.aspx/ConstructUrl",
data: jsonData,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
window.open("https://www.myurl.com");
},
error: function(msg) {
//alert(error);
}
});
请注意,您可能必须为此将 $.ajax 的异步选项设置为 false,否则可能会在收到响应之前评估 $.ajax 调用之后的代码.
Note you might have to set $.ajax's async option to false for that, otherwise the code following the $.ajax call could be evaluated before the response is received.
这篇关于在 jQuery ajax 成功回调中创建时弹出窗口被阻止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 jQuery ajax 成功回调中创建时弹出窗口被阻止


- Flexslider 箭头未正确显示 2022-01-01
- Fetch API 如何获取响应体? 2022-01-01
- Quasar 2+Apollo:错误:找不到ID为默认的Apollo客户端。如果您在组件设置之外,请使用ProvideApolloClient() 2022-01-01
- 使用RSelum从网站(报纸档案)中抓取多个网页 2022-09-06
- CSS媒体查询(最大高度)不起作用,但为什么? 2022-01-01
- Css:将嵌套元素定位在父元素边界之外一点 2022-09-07
- 如何使用 JSON 格式的 jQuery AJAX 从 .cfm 页面输出查 2022-01-01
- 失败的 Canvas 360 jquery 插件 2022-01-01
- addEventListener 在 IE 11 中不起作用 2022-01-01
- 400或500级别的HTTP响应 2022-01-01