3

My question: My ajax request downloads a generated file as expected but never fires the success callback and fires the complete callback immediately. How do I get a function to be triggered by the completed download of the file?

Some details: I have a form that submits to itself and calls a function that generates a file for download.

The ajax:

var prepdata = 'start=' + startdata + '&end=' + enddata;
$.ajax({
    url: 'index.php',
    data: prepdata,
    success: function(){console.log('success.');},
    complete: function(){console.log('complete')}
});

So you submit a form (in index.php), the form submits to the same page (index.php) and passes some $_GET paramaters. The presence of the $_GET params triggers a function that gets some data from the database, formats a file and outputs it with:

header("Content-type: application/vnd.ms-excel");
// more headers here... the correct ones, trust me
print $data;
exit;

Now, all of this works, resulting in a downloaded file. The problem is that I want to fire a javascript function (that removes a "...loading" symbol) after the page has been downloaded. What happens is that the "success" callback is never triggered while the "complete" call back is triggered immediately after submitting.

How do I trigger a function after the data has been downloaded?

Any ideas? One more thing, in Safari I see the following console error:

GET http://mysite.com/somedir/?start=1&end=2 Frame load interrupted

Which I assume has something to do with the headers being output and interrupting the flow of things, and which I assume is why the ajax is failing. Just don't know how to work around it.


EDIT: Nothing worked. :( I tried loading into an iframe but couldn't get the load event to trigger because of the frame load interrupted issue. In the end I just faked it with a timeout that runs after a set period that is slightly longer than the longest download I can reasonably expect.

rg88
  • 20,742
  • 18
  • 76
  • 110
  • 1
    When you check the Firebug console does the request come back successful? – Seth Sep 27 '11 at 05:09
  • http://stackoverflow.com/q/149821/413670 – Rafay Sep 27 '11 at 05:14
  • @Seth - No. The GET request is highlighted red as if it failed, however I see the complete callback has fired and the data has been downloaded in the correct format. – rg88 Sep 27 '11 at 05:16
  • @3nigma - That isn't the same question. Assuming I do that, how do I trigger a javascript function after this is done? I used ajax so I could hook into the success or complete callbacks, how do I trigger something without ajax and these callbacks? – rg88 Sep 27 '11 at 05:27
  • what are you getting back from the server? html? json? – Rafay Sep 27 '11 at 05:41
  • @3nigma - comma separated values that are being returned as MIME type application/vnd.ms-excel – rg88 Sep 27 '11 at 05:51
  • if you just want to remove the loading symbol you can call the success function without any arguments and/or specify a `.complete` callback, like `complete:function(){//remove the symbol here}` or you can use `$.ajaxComplete(function(){//remove symbol here});` – Rafay Sep 27 '11 at 06:05
  • @3nigma - I did specify a `complete` callback. I explained how this wasn't working in my question. – rg88 Sep 27 '11 at 06:18
  • my bad i didnt read the question carefully ... – Rafay Sep 27 '11 at 06:23
  • 1
    +1 as I've got a related issue, suspecting a Safari bug. I have a GET form with an image submit button, and on submission I do a "Location: X" on the server to redirect to a page that sets headers and outputs downloadable file data. Of course this leaves the original page/form on-screen, with "Frame load interrupted" in the Safari console. Accordingly the submission just works once, and then stops working - seems like page initialisation breaks for this sequence. Works fine in Chrome and FF7 on OSX. Will submit a bug report to Safari devs. – halfer Oct 05 '11 at 11:28
  • @gaoshan88 Hi, did you find any solution to this prob? I am also stuck on the same. – Ankur Oct 11 '11 at 11:23
  • @AnkurMittal No Ankur, I never did. Sorry. – rg88 Oct 14 '11 at 07:17

1 Answers1

1

Hmm, I believe I have fixed my similar situation (although mine doesn't use ajax, I am setting HTTP headers to download data as a result of a submit). Random punt: try changing your form from GET to POST. Still think it is a Safari bug, as it doesn't appear to affect FF or Chrome.

halfer
  • 19,824
  • 17
  • 99
  • 186