javascript - 如何在ajax调用后返回的html页面上调用window.print

标签 javascript ajax laravel printing

我在引导模式中有一个打印按钮。

$('#printST').click(function () {
    $.ajax({
        type: 'GET',
        url: 'print',
        data: formData,
        dataType: 'html',
        success: function (html) {
           // how to print the content of html?
        },
        error: function (data) {
            console.log('Error:', data);
        }
    });
});

我要打印返回的数据。

在我的打印页面中

<script type="text/javascript">
window.onload = function() { window.print(); }

在我的 Controller 中

...
    return View::make('pages.print');

如何在调用 ajax 后打印此页面的内容?

最佳答案

调用并返回 return View::make('pages.print'); 将无济于事,因为它只会创建 View 类的实例。你需要 HTML 字符串,为此你需要调用 render()

所以对于你的 Controller

 $view = View::make('pages.print'); 
 return $view->render();

它将HTML 字符串返回给Ajax 函数

在您的 Ajax 调用中

$('#printST').click(function () {
    $.ajax({
        type: 'GET',
        url: 'print',
        data: formData,
        dataType: 'html',
        success: function (html) {
            w = window.open(window.location.href,"_blank");
            w.document.open();
            w.document.write(html);
            w.document.close();
            w.window.print();
        },
        error: function (data) {
            console.log('Error:', data);
        }
    });
});

它将打开打印窗口,用于从 Controller 返回HTML

希望这会有所帮助!

关于javascript - 如何在ajax调用后返回的html页面上调用window.print,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46065613/

相关文章:

javascript - 如何从不同的数组中获取按属性排序的对象?

ruby-on-rails - 如何确保向 Rails 发送此发布请求?

php - Laravel Eloquent 地返回一个无符号整数列的记录限制为 2147483647

Laravel/Eloquent whereIn/where 带有一对集合

javascript - 如何调整 Vimeo 播放器的大小?

javascript - 使用 for 循环和 document.getElementById 在 Javascript 中填充数组

javascript - 如何从给定的身份验证中解码

javascript - 单击按钮时如何从数据库中一次检索 1 行?

jQuery Ajax 请求不返回数据

php - Laravel 队列 - 释放内存