I have an action class that generates a PDF. The contentType is set appropriately.

public class MyAction extends ActionSupport 
{
   public String execute() {
    ...
    ...
    File report = signedPdfExporter.generateReport(xyzData, props);

    inputStream = new FileInputStream(report);
    contentDisposition = "attachment=\"" + report.getName() + "\"";
    contentType = "application/pdf";
    return SUCCESS;
   }
}

我通过Ajax调用来调用这个action.我不知道如何将这个流传送到浏览器.我试了几次,但都没用.

$.ajax({
    type: "POST",
    url: url,
    data: wireIdList,
    cache: false,
    success: function(response)
    {
        alert('got response');
        window.open(response);
    },
    error: function (XMLHttpRequest, textStatus, errorThrown) 
    {
        alert('Error occurred while opening fax template' 
              + getAjaxErrorString(textStatus, errorThrown));
    }
});

上面给出了错误:

您的浏览器发送了此服务器无法理解的请求.

推荐答案

You don't necessarily need Ajax for this. Just an <a> link is enough if you set the content-disposition to attachment in the server side code. This way the parent page will just stay open, if that was your major concern (why would you unnecessarily have chosen Ajax for this otherwise?). Besides, there is no way to handle this nicely acynchronously. PDF is not character data. It's binary data. You can't do stuff like $(element).load(). You want to use completely new request for this. For that <a href="pdfservlet/filename.pdf">pdf</a> is perfectly suitable.

为了更好地帮助您使用服务器端代码,您需要更多地介绍所使用的语言,并发布代码摘要.

Jquery相关问答推荐

try 使用jQuery AJAX将参数传递给http Post方法时出现未知错误

我在自定义 WordPress 插件中使用 ajax 时收到错误请求

如何向父元素添加类?

DataTables - this.api 用于回调函数中的多个表

$ 不是函数 - jQuery 错误

获取单选按钮组的值

如何在使用 jQuery 单击特定链接时打开 bootstrap 导航选项卡的特定选项卡?

可以在删除类时反转css动画吗?

Ajax 更新后在 jQuery 中重新绑定事件(更新面板)

为特定请求禁用 ajaxStart() 和 ajaxStop()

如何立即启动 setInterval 循环?

即使通过Javascript代码判断,如何触发复选框单击事件?

为 jQuery AJAX 调用实现加载指示器

CSS3 相当于 jQuery slideUp 和 slideDown?

你会如何比较 jQuery 对象?

如何插入元素作为第一个子元素?

D3 和​​ jQuery 有什么区别?

自调用函数前的分号?

如何使用 jQuery 或仅使用 Javascript 将按钮重定向到另一个页面

jQuery:通过 .attr() 添加两个属性;方法