1

I am trying to track all requests made through an application by setting a custom request header on all outgoing requests. I was able to accomplish this using:

$ajax.Setup(
    beforeSend: ...

However, I ran into an issue when the src attribute on a dom element (i.e. iframe, video, object, etc) was initiating the request. When searching I had found: How to set custom http headers when changing iframe src? However, when I attempted this, I was unable to get it to initiate the download dialog. That code looked like:

var request = $.ajax({
    context: this,
    url: URI,
    type: 'GET',
    statusCode: {
       200: function(data) {
           var id = '#download-iframe';
           var $iframe = $(id);
           if($iframe.length === 0) {
              $iframe = $('<iframe>')
                 .attr('id', 'download-iframe')
                 .appendTo('body');
           }
           $iframe.contents().find('html').html(data);
        }
     }
});

The response headers for that request look as follows:

Content-Disposition:attachment; filename="PDF.pdf"
Content-Length:86712
Content-Type:application/pdf;charset=UTF-8

This displayed the raw data in the iframe, but did not initiate the download.

Is there anyway to either A) (preferred) Set the request headers a different way via setting the DOM element src or B) Get the download dialog to initiate.

Community
  • 1
  • 1
user856354
  • 273
  • 3
  • 6
  • 21
  • You write the response into the iframe using Javascript? That won't give you a download option. Let the iframe load it itself - create and insert an iframe with the src set to the URL that sends the PDF. I'm confused by your problem description though so I may be talking about the weather on Pluto... – Mörre Jan 13 '15 at 20:13
  • That's what I was originally doing, when I set the src to the URL though then the DOM kicked off the request, bypassing the javascript so the request header configured in ajaxSetup never got set. – user856354 Jan 13 '15 at 20:17
  • In that case it won't work. But instead of the request header, why not add a parameter to the URL? So you can still track it. In any case, writing stuff into the iframe is not a download - how would the browser know, which is the one controlling that download/save file dialog? – Mörre Jan 13 '15 at 20:25
  • Yea, that's along the line of what I was thinking, which is why I was thinking option A was preferred, setting the request header on a dom initiated request another way. – user856354 Jan 13 '15 at 20:31

1 Answers1

0

B) Get the download dialog to initiate.

blob URI + <a download ...> + synthesizing a click event will probably work

the8472
  • 40,999
  • 5
  • 70
  • 122