mar*_*ith 12 ajax jquery popup
任何人都可以帮助,我有一些jquery和chrome阻止我正在创建的弹出窗口.经过一些调查后,似乎是一个window.open成功发生ajax调用的问题.这有什么方法吗?我的jquery ajax调用需要返回我需要打开的URL,所以我有点卡住了.
如果我放置window.openajax调用的外部它可以工作,但在内部(成功)它被阻止.我认为这与CONTEXT有关,但我不确定......
任何想法真的很感激......
这是我有的:
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);
}
});
Run Code Online (Sandbox Code Playgroud)
And*_*rew 17
有几个人指出,接受的答案不再有效.根据aidiakapi的评论,我首先打开窗口使用了一种解决方法.
window.open("about:blank", "myNewPage");
Run Code Online (Sandbox Code Playgroud)
然后执行ajax业务并在done()函数中打开具有该名称的页面.
window.open("/foo.html", "myNewPage");
Run Code Online (Sandbox Code Playgroud)
如果您执行异步操作也无关紧要.
只需在成功回调中打开新窗口:
$.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);
}
});
Run Code Online (Sandbox Code Playgroud)
请注意,您可能必须将$ .ajax的async选项设置为false,否则可以在收到响应之前评估$ .ajax调用之后的代码.
你的代码在firefox和chrome中返回:
Firefox:"Firefox阻止该网站打开弹出窗口." Chrome:"弹出窗口被阻止"
要解决此问题,只需在您的ajax调用中添加async:false即可.
$.ajax({
type: "POST",
async: false,
url: "MyService.aspx/ConstructUrl",
data: jsonData,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(url) {
window.open(url);
},
error: function(msg) {
//alert(error);
}
Run Code Online (Sandbox Code Playgroud)
});
请注意,async:false会强制javascript等待jQuery ajax结果.这意味着它冻结javascript直到ajax调用完成.
| 归档时间: |
|
| 查看次数: |
34828 次 |
| 最近记录: |