Google Chrome blocks ajax requests when print preview is opened on child window

后端 未结 4 1757
迷失自我
迷失自我 2020-12-09 09:46

There are 2 files: index.html and print.html

First one contains a button that opens print.html using simple command:



        
相关标签:
4条回答
  • I had similar issue with Chrome - because of the security policy it can't access local files. When I am making an AJAX call I get this error

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

    From what I know - you should launch Chrome with params:

    --allow-file-access-from-files
    

    Hope it helps.

    0 讨论(0)
  • 2020-12-09 10:23

    You need to install mod_headers on Apache and set it on .htaccess

    Header add Access-Control-Allow-Origin "*"
    
    0 讨论(0)
  • 2020-12-09 10:28

    there is a Chrome bug where window.print() does not work when there is a tag in the DOM. It might be solved by calling this function:

    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 讨论(0)
  • 2020-12-09 10:31

    Your server not added ORIGIN headers. You need add it on .htaccess. For example:

    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"
    

    Or you can add it in PHP on print.html (if you can use PHP on html files)

    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");
    
    0 讨论(0)
提交回复
热议问题