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 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)