如何使用soap webservices在javascript中下载pdf?

时间:2015-12-15 12:16:43

标签: javascript php web-services pdf soap

我的表中有一个账单清单,每一行都有自己独特的账单(不同的日期,金额等......)。我已成功设法通过Laravel 5中的Soap:Wrapper进行php调用,当我输入浏览器“路径到该程序”时,我得到了我的pdf。但我想用Javascript打开它。

我的服务器端代码

$pdfData= $soap->racun($data);

header("Content-type: application/pdf");
header("Content-Length:" . strlen($pdfData));

//this line if i want to preview my pdf in browser 
header("Content-Disposition: inline; filename=pdf.pdf");

//this line if i want to download my pdf
header('Content-Disposition: attachment; filename="pdf.pdf"');
print $pdfData;

我的客户端代码是

$.ajax({
     type:"GET",
     url:"/path",
     data:"param1=" + par1+"&param2="+ par2+ "&param3="+par3,
     success:function(msg){
               //alert(msg);
               //here needs to show that pdf but don't know how 
               document.write(msg);
     },
     fail:function(){
                alert('Error with pdf');                    
     }
  });

因此,当用户点击链接获取他的账单时,我发送AJAX调用并使用SOAP WebService获取该pdf。问题是,如果我在浏览器中编写/path,我可以预览或下载它,但不知道如何在Javascript中执行此操作。我做错了什么?

1 个答案:

答案 0 :(得分:2)

嗯,您正在发送GET请求,因此您只需执行此操作:

window.location = "/path?" + "param1=" + par1+ "&param2=" + par2 + "&param3=" + par3;

您可能还想查看this answer