vendredi 30 janvier 2015

Catching an error response when loading content for a new window javascript


I have an input element which triggers a popup with a rendered pdf. However, generation of this pdf is going to fail under certain conditions. I would like to catch the resultant error in the original window and render the popup only if the request succeeds. This is trivial to do if I make two requests:



$.ajax({
method: 'GET',
url: url,
success: function() {
window.open(url);
},
error: function() {
...
}
});


But, I'd like to avoid that second request since this pdf generation is quite expensive. I've currently tried the following:


Create empty window, write pdf to DOM, pushState().



success: function(r) {
var w = window.open();
w.docuent.write(r);
w.history.pushState('init', 'A PDF!', url);
}


This fails since r contains a PDF bytestring.


Open a window with the bytestring, then pushState().



success: function(r) {
var w = window.open('data:application/pdf,' + escape(r));
w.history.pushState('init', 'A PDF!', domain + url);
}


This fails since the pushState overwrites the initial stream data.


How can I create a new window with the rendered PDF (without using any external library other than jquery) using the response data and then set its url correctly?





Aucun commentaire:

Enregistrer un commentaire