最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Printing page from AJAX - Stack Overflow

programmeradmin4浏览0评论

I wrote a simple generator to create offers. It works good, but I got the problem with printing. When I try a print page from AJAX request, I getting index page not data from AJAX request.

What's wrong?

Additionally, 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 PHP file :

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

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

<?php 

$result = ob_get_flush();
echo $result;

?>

I wrote a simple generator to create offers. It works good, but I got the problem with printing. When I try a print page from AJAX request, I getting index page not data from AJAX request.

What's wrong?

Additionally, 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 PHP file :

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

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

<?php 

$result = ob_get_flush();
echo $result;

?>
Share Improve this question edited Dec 18, 2017 at 8:42 Junius L 16.2k6 gold badges58 silver badges102 bronze badges asked Dec 18, 2017 at 8:32 sauerosauero 2592 silver badges15 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

The print() method doesn't take any arguments. So, if you want to print data from 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();
});

this will open a new tab, with value of data on screen and ready to print.

The print() method:

Opens the Print Dialog to print the current document.

It doesn't take any arguments (so any arguments you pass it will be ignored).

If you want to print data you have in a variable, you need to put it in the document the user is viewing.

发布评论

评论列表(0)

  1. 暂无评论