javascript - 我想在模态窗口中显示 PDF ajax 响应

标签 javascript html jquery

使用 ajax,我得到的响应是 PDF base64 数据。在此示例中,我在新窗口中显示它,而不是我想在模式弹出窗口中显示 PDF。有什么办法吗?

$.ajax({
        type : "POST",
        url : getPrintablePDF,
        dataType : "json",
        contentType : 'application/json; charset=utf-8',
        data : JSON.stringify(params),
        success : function(data) {
            //console.log(data);
            var myResponse = eval(data);
            window.open('data:application/pdf;base64,' + myResponse.base64EncodedResponse);

        }
    });     

最佳答案

尝试使用 <object /><embed />或使用 <iframe />

我用 <iframe /> 举了一个例子因为所有浏览器都支持它。

$.ajax({
    type : "POST",
    url : getPrintablePDF,
    dataType : "json",
    contentType : 'application/json; charset=utf-8',
    data : JSON.stringify(params),
    success : function(data) {
        var myResponse = eval(data);
        $("<iframe />") // create an iframe
          // add the source
          .attr('src', 'data:application/pdf;base64,' + myResponse.base64EncodedResponse)
          .appendTo('.modal-body'); // append to modal body or wherever you want
    }
});    

关于javascript - 我想在模态窗口中显示 PDF ajax 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14153207/

相关文章:

jQuery 的延迟回调执行顺序

javascript - 将代理添加到 X Ray Node JS

javascript - Node.js 服务器循环中的 SQL 查询

javascript - 单击 anchor 标记后,显示 AngularJS ng-repeat 给出的值

html - "document type does not allow element "脚本 "here"尝试验证我的页面时

javascript - 单击html中的链接没有给出响应

java - 如何动态填充jsp页面?

javascript - angular 2 ngfor 在数组中显示组件

javascript - Jasmine/Jest 类型冲突

javascript - 递归图像处理(Canvas、HTML5、Javascript)