我有一个链接,当用户点击它时,他会得到一个 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 ,响应:
在浏览器中响应...
我已经尝试在服务器中设置内容类型,但没有成功:
编辑:执行此操作不需要 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/