[英]Call php file which return PDF with jQuery AJAX function
My php file: example_061.php create PDF return and everything works fine. 我的PHP文件:example_061.php创建PDF返回,一切正常。
Now I want to call that file with jQuery AJAX to get this at my screen and I try: 现在,我想使用jQuery AJAX调用该文件,以使其在屏幕上显示,然后尝试:
$("#proba").click(function() {
//in here we can do the ajax after validating the field isn't empty.
$.ajax({
url: "tcpdf/examples/example_061.php",
type: "POST",
async: true,
data: {
ime: $("#ime").val(),
pozicija: $("#pozicija").val(),
jmbg: $("#jmbg").val()
}, //your form data to post goes here as a json object
dataType: "html",
success: function(response) {
window.open('example_061.pdf');
},
});
});
Everything is fine so success function work an I get alert message but I don't get PDF file as download or at screen. 一切都很好,因此成功功能可以正常工作,我会收到警报消息,但下载或在屏幕上都看不到PDF文件。 How I can do that?
我该怎么做?
You do not need to use AJAX to download a file. 您不需要使用AJAX来下载文件。 If the
content-disposition
header is set, it will be downloaded and the current page will not be reloaded. 如果设置了
content-disposition
标头,则将下载它,并且不会重新加载当前页面。
Just add this to your php script that creates the PDF: 只需将其添加到创建PDF的php脚本中:
header('Content-Disposition: attachment; filename=' . $MyFileName);
And instead of AJAX, use a regular anchor tag link: 而不是AJAX,请使用常规锚标记链接:
<a href="tcpdf/examples/example_061.php">Download</a>
As someone noted in the comments, if you need to post you can still use a simple form with the same effect: 正如评论中指出的那样,如果您需要发布,仍然可以使用具有相同效果的简单表单:
<form method="POST" ACTION="tcpdf/examples/example_061.php">
<input type="hidden" name="myPostItem" value="My POSt VALUE" />
<input type="submit" value="download">
</form>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.