在新窗口中显示POST AJAX响应 [英] Displaying POST AJAX Response in new window

查看:120
本文介绍了在新窗口中显示POST AJAX响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用jQuery通过ajax发送POST数据,它返回PDF文件的二进制数据。

I send POST data through ajax using jQuery and it returns the binary data of a PDF file.

我想对这些数据做些什么。通过提供下载链接,或打开一个新的选项卡/窗口。

I'd like to do something with this data. Either by providing a link to download, or opening it a new tab/window.

我知道将用户发送到网站并使用GET变量会更容易但是有很多数据都是通过发送的,需要发布。

I know sending a user to a website and using GET variables would be easier but there is a lot of data that gets sent through and it needs to be post.

有没有办法可以获取我检索到的数据并让用户下载/看到它某种程度上?

Is there a way I can take the data that I retrieve and let a user download / see it somehow?

$("#export_pdf").click(function()
{
    var data_serialize = "v1=blah&var2=again&var3=more_info",
    url = "/actions/export-pdf";
    $.ajax({ 
        type: "POST",
        url: url,
        data: data_serialize,
        success: function(data)
        {   
            // data = PDF binary
            // I want to do something with this
        },error: function(data) { alert("error"); }
    });
});


推荐答案

我假设你能够检索数据并且它存储在数据变量中。

I am assuming that you are able to retrieve the data and it is being stored in the data variable.

您可以在成功函数中尝试以下操作:

You might try the following in your success function:

       //open a new window note:this is a popup so it may be blocked by your browser
       var newWindow = window.open("", "new window", "width=200, height=100");

       //write the data to the document of the newWindow
       newWindow.document.write(data);

另一种选择是动态创建一个div,使用$ .ajax中的成功函数显示函数。

Another option would be to dynamically create a div that is displayed using your success function in your $.ajax function.

这是一个jsfiddle,如果你想试验它会帮助你:

Here is a jsfiddle that will help you if you want to experiment with it:

http://jsfiddle.net/larryjoelane/d8r3su0m/

我已经使用jquery $ get函数测试了另一种方法,它适用于我。你只需要启用弹出窗口。这是我测试的代码。当我有机会的时候,我会尝试用你的ajax功能。

I have tested another way with the jquery $get function and it works for me. You just have to enable popups. here is the code I tested. I will attempt it with you ajax function when I get a chance.

    $("#load_pdf").on("click",function(){


//swap url with a pdf file you have access to for testing
var url = "http://localhost/Responder Manual.pdf";


$.get(url,function(data){


       //open a new window note:this is a popup so it may be blocked by your browser
       var newWindow = window.open("", "new window", "width=200, height=100");

       //write the data to the document of the newWindow
       newWindow.document.write(data);


});

});

这篇关于在新窗口中显示POST AJAX响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆