php - jQuery Ajax : pdf response

标签 php jquery html ajax mime-types

我有一个链接,当用户点击它时,他会得到一个 PDF。在 jQuery 中,我创建了一个对服务器的 POST ajax 调用以获取 PDF。响应是一个 PDF,带有正确的内容标题等,通常会导致浏览器打开 Reader 插件,或允许用户保存 PDF。但就我而言,这是行不通的。有没有办法设置数据内容类型,或者设置内容类型为PDF?

我的 ajax 调用:

$('#sf_getpdf').click(function() {

 $.ajax({    //create an ajax request to load_page.php

        type: "POST",

        url: "index.php?route=sale/order/superfaktura_getpdf&token=<?php echo $token; ?>",

        data: 'invoice_id=<?php echo $invoice_id; ?>&sf_token=<?php echo $sf_token ?>',  //with the page number as a parameter

        dataType: "text",   //expect html to be returned


        success: function(msg){



            if(parseInt(msg)!=0)    //if no errors

            {

            document.write(msg)
            }

        }

    });

});

Firebug ,响应: Firebug

在浏览器中响应...

response in browser

我已经尝试在服务器中设置内容类型,但没有成功:

content-type in server

编辑:执行此操作不需要 Ajax。

<a id="adr_stitok" target="_blank" href="index.php?route=sale/order/superfaktura_getpdf&token=<?php echo $token; ?>&invoice_id=<?php echo $invoice_id; ?>&sf_token=<?php echo $sf_token ?>" >Download</a></td>

会做事。

最佳答案

确保您的服务器端返回可下载的内容并提交到文件,例如:

            //$.download('path', 'data' [, 'post'])
            $.download = function(url, data, method) {
                //url and data options required
                if(url && data) {
                    var form = $('<form />', { action: url, method: (method || 'get') });
                    $.each(data, function(key, value) {
                        var input = $('<input />', {
                            type: 'hidden',
                            name: key,
                            value: value
                        }).appendTo(form);
                    });
                return form.appendTo('body').submit().remove();
                }
            throw new Error('$.download(url, data) - url or data invalid');
            };

$.download("index.php?route=sale/order/superfaktura_getpdf&token=<?php echo $token; ?>", {}, 'post')

关于php - jQuery Ajax : pdf response,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10627051/

相关文章:

phpunit 只显示帮助/使用信息,没有测试结果

php - 防止JSON域欺骗

javascript - 创建横向滚动网站

html - PhpStorm 中 PHP 文件中的 HTML 缩进

javascript - 如何添加按下时切换视频声音的按钮?

php - 一对多关系的 Symfony2 createQueryBuilder

php - 如何按小时拆分 PHP 数组

jquery - 将类添加到具有匹配自定义数据属性的元素

jquery - 在移动 View 中无法弄清楚如何使我的菜单按钮工作

jquery - CSS 3Dtransform z-index