Printing a page with AJAX

I wrote a simple generator for creating sentences. It works well, but I am having a printing problem. When I try to print a page from an AJAX request, I get the index page, not the data from the AJAX request.

What's wrong?

Also, the data from AJAX is correct.

$.post({
    type: "POST",
    url: "generate.php",
    data: {pid: pid, net: net, brutto: brutto, contractor: contractor, delivery: delivery, term: term},
}).done(function(data) {
    window.print(data);
});

Example for a PHP file:

<?php
$foo = "foo";
ob_start();
?>

<p><?php echo $foo; ?></p>

<?php 

$result = ob_get_flush();
echo $result;

?>
+4
source share
2 answers

The method print()does not accept any arguments. So, if you want to print data from a variable, you can use this method:

$.post({
    type: "POST",
    url: "generate.php",
    data: {pid: pid, net: net, brutto: brutto, contractor: contractor, delivery: delivery, term: term},
}).done(function(data) {
    printWindow = window.open('');
    printWindow.document.write(data);
    printWindow.print();
});

a new tab opens with the value dataon the screen and is ready to print. A.

+4
source

print()method :

Opens the print dialog for printing the current document.

( , , ).

, , , .

+3

Source: https://habr.com/ru/post/1690955/


All Articles