开发者

jQuery - Ajax call failing to reach success callback but still functioning

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('com开发者_开发知识库plete')}
});

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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜