2013-01-22 10 views
19

有2个文件:index.htmlprint.html谷歌浏览器块时打印预览打开子窗口中的Ajax请求

首先一个包含了使用简单的命令打开print.html按钮:

window.open("print.html", "_blank", "menubar=yes,toolbar=yes,status,scrollbars,resizable"); 

print.html仅含一个打开打印预览对话框的按钮:

<button onclick="window.print();"> 

问题出现当打印预览对话框打开时。在这种情况下,对index.html的任何操作 - 即启动ajax请求的其他文件 - 将被临时阻止并放入队列中。只有在预览关闭的情况下,浏览器才会触发所有请求。

我只能在谷歌浏览器(24.0.1312.52米)中看到它。

有人可以证实这是Chrome的错误吗?

+1

纠正我,如果我错了,但这听起来不像是我的错误。打印预览不是模态对话框吗?为什么Ajax请求在它打开时触发? –

+2

index.html和print.html是两个不同的窗口。使用window.open.Print预览在print.html上打开索引print.html。为什么index.html被阻止?打印窗口被阻止是可以的。 –

+0

啊,我明白了。这听起来很奇怪。 –

回答

0

我与Chrome有类似的问题 - 由于安全策略,它无法访问本地文件。 当我在做一个AJAX调用我得到这个错误

XMLHttpRequest cannot load file:///*. Origin null is not allowed by Access-Control-Allow-Origin. 

从我所知道的 - 你应该使用参数运行Chrome浏览器:

--allow-file-access-from-files 

希望它能帮助。

+0

与John有同样的问题,我不敢说“--allow-file-access-from-files”没有解决问题:/ – stuXnet

0

创建一个.bat文件,写下面几行。第一行是Chrome应用程序的路径,然后将其打开。现在你可以检查这个Chrome。

cd "C:\Program Files (x86)\Google\Chrome\Application" 
chrome --allow-file-access-from-files --disable-web-security 
2

存在Chrome漏洞,其中window.print()在DOM中有标记时不起作用。它可能通过调用此功能来解决:

function printPage() { 
    window.print(); 

    //workaround for Chrome bug - https://code.google.com/p/chromium/issues/detail?id=141633 
    if (window.stop) { 
     location.reload(); //triggering unload (e.g. reloading the page) makes the print dialog appear 
     window.stop(); //immediately stop reloading 
    } 
    return false; 
} 
+0

我遇到了类似的问题,这发生在Chrome版本37.0 .2062.103米。您的解决方案只能使用一次使用相同的页面实例进行重试,并且打印对话框没有从window.open的页面打开。我把它搞砸了,因为这是一个很好的尝试,也许可以开展工作。 –

+0

对于那些有兴趣的人,[这里](https://code.google.com/p/chromium/issues/detail?id=359627)是存档的错误...它说它已在Chrome的v34和v35中修复,但从此以后再次出现。 – incutonez

+0

我有相同的问题,但有2个单独的选项卡(请参阅http://stackoverflow.com/questions/42932835/angular-js-print-tab-blocks-http-request-google-chrome)。我如何在我的情况下使用这种解决方法? – molerat

1

您的服务器未添加ORIGIN标头。 您需要将其添加到.htaccess。例如:

Header add Access-Control-Allow-Origin "*" 
Header add Access-Control-Allow-Headers "origin, x-requested-with, content-type" 
Header add Access-Control-Allow-Methods "PUT, GET, POST, DELETE, OPTIONS" 

或者你也可以在PHP上print.html添加它您需要在.htaccess在Apache安装mod_headers并设置(如果你可以在HTML文件中使用PHP)

header ("Access-Control-Allow-Origin: *"); 
header ("Access-Control-Allow-Headers: origin, x-requested-with, content-type"); 
header ("Access-Control-Allow-Methods: PUT, GET, POST, DELETE, OPTIONS"); 
1

Header add Access-Control-Allow-Origin "*"