1

I´m trying to have a loading image while waiting for the server to generate a PDF. When PDF is generated a browser dialog is prompted and that´s when the loading image should hide.

$.ajax({
  type: 'POST',
  url: $(this).attr('href'),
  success: function(response)
  {
   console.log( response );
   $(".report-generator").submit();
  },
 }
});

So this code is working.. kind of.. the loading image is shown but only for a short period of time.. the ajax call ends before the file is sent to the user.

This code shows the ajax loader when ever a ajax function is called

function initAjaxLoader() {
    $(document.body).ajaxStart(function() { 
        $('#ajax-loader').show();
    }).ajaxStop(function() {
        $('#ajax-loader').hide();
    });
}

So how do I keep the ajax call "active" until the download is prompted?

Mackelito
  • 4,213
  • 5
  • 39
  • 78
  • You can't keep it active since the javascript runs on the client side ! – mas-designs Mar 02 '12 at 12:24
  • But can´t I make ajax wait and listen for a respos (file sent) by the server? – Mackelito Mar 02 '12 at 13:06
  • 1
    No you can't do that, you could after you "initiate" the generating of the .pdf set a Timeout for 20 seconds and then do the second ajax which loads the .pdf but that's really really crappy – mas-designs Mar 02 '12 at 13:46

1 Answers1

0
$.ajax({
  type: 'POST',
  url: $(this).attr('href'),
  success: function(response)
  {
   console.log( response );
    $('#result').load('test.pdf', function() {
        $('#ajax-loader').hide();
     });
  },
 }
});
sandeep
  • 2,244
  • 1
  • 23
  • 38