Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Http request for file download stops JQuery ajax calls

Problem:

On the site I am building, I have two JQuery ajax long polling calls constantly pending. I am now trying to put in a file download feature, so that when link is pressed the user is prompted with the SaveAs box. This download of the file is working fine, the problem is that when the link is pressed the two ajax calls are cancelled.

I am trying either not have the ajax calls cancelled or the possibility of setting up the ajax calls straight away.

Here is the code for the link:

HTML:

<a href="/test" id="testfile">Tasks</a>

JS:

$(document).on('click',"#testfile",function(event){
    event.preventDefault();
    var href=$(this).attr('href');
    window.location.href = href;
    updater();                               /* AJAX CALL #1 */
    notifier();                              /* AJAX CALL #2 */
});

What I have tried:

I the code above I was trying to call the two ajax calls after starting the download. This is not working. If I put in some delay so that the calls are initiated after the download is complete then it works but seeing as the files can be big and therefore take an unknown amount of time to receive this is of course a bad solution.

I am really confused as to why the ajax calls are being cancelled? Is this because the page is being unloaded when I press the link?

Seeing as I am only running on a development server with one thread, the new ajax calls that I am trying to set up right after following the link might be failing because the server is busy, could this be the case?


SOLVED:

I added a hidden iframe to the buttom of the site and with targeted that with my link. I also removed the JS code, since the ajax calls are now not being cancelled.

The code is now looking like this:

HTML:

<a href="/test" target="filetargetframe"  id="testfile">Tasks</a>

********

<iframe name="filetargetframe" style="display:none"></iframe>
like image 215
ReturnToZero Avatar asked Oct 30 '13 15:10

ReturnToZero


1 Answers

Using the answer from yoavmatchulsky the problem was solved:

I added a hidden iframe to the buttom of the site and with targeted that with my link. I also removed the JS code, since the ajax calls are now not being cancelled.

The code is now looking like this:

HTML:

<a href="/test" target="filetargetframe"  id="testfile">Tasks</a>

********

<iframe name="filetargetframe" style="display:none"></iframe>
like image 136
ReturnToZero Avatar answered Sep 29 '22 02:09

ReturnToZero