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.
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"); }
});
});
ajax({ url, data: data, type: "POST", dataType: "json", success: function(cbdata) { update_table(cbdata); } }); } $('#selector'). on('click', changeDate); So you can call changeData() when you need it.
GET is basically used for just getting (retrieving) some data from the server. Note: The GET method may return cached data. POST can also be used to get some data from the server. However, the POST method NEVER caches data, and is often used to send data along with the request.
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);
Another option would be to dynamically create a div that is displayed using your success function in your $.ajax function.
Here is a jsfiddle that will help you if you want to experiment with it:
http://jsfiddle.net/larryjoelane/d8r3su0m/
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);
});
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With