douwan2664
2015-05-04 13:59
浏览 99

使用jQuery AJAX函数调用返回PDF的php文件

My php file: example_061.php create PDF return and everything works fine.

Now I want to call that file with jQuery AJAX to get this at my screen and I try:

$("#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. How I can do that?

图片转代码服务由CSDN问答提供 功能建议

我的php文件:example_061.php创建PDF返回,一切正常。 < p>现在我想用jQuery AJAX调用该文件在我的屏幕上显示并尝试:

  $(“#proba”)。click(function(){  
 //在这里我们可以在验证字段不为空后执行ajax。
 $ .ajax({
 url:“tcpdf / examples / example_061.php”,
 type:“POST”,\  n async:true,
 data:{
 ime:$(“#ime”)。val(),
 pozicija:$(“#pozicija”)。val(),
 jmbg:$(“#  jmbg“)。val()
},//你要发布的表单数据作为json对象发送到这里
 dataType:”html“,
 success:function(response){
 window.open('example_061。  pdf'); 
},
}); 
}); 
   
 
 

一切都很好,所以成功功能正常工作,我收到提醒信息,但我不知道 不要将PDF文件作为下载或屏幕显示。 我怎么能这样做?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dqwh0109 2015-05-04 14:04
    已采纳

    You do not need to use AJAX to download a file. If the content-disposition header is set, it will be downloaded and the current page will not be reloaded.

    Just add this to your php script that creates the PDF:

    header('Content-Disposition: attachment; filename=' . $MyFileName); 
    

    And instead of AJAX, use a regular anchor tag link:

    <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>
    
    点赞 评论

相关推荐 更多相似问题