douwan2664 2015-05-04 13:59
浏览 101
已采纳

使用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?

  • 写回答

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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料