简体   繁体   中英

Ajax request not aborted on page redirect

I am sending ajax request on particular page. and i click on another link. and response of ajax request is not come. there is any way to not destroy my ajax request on click on any other link.

If the user leaves the page, there's not much you can do about that. You can try to discourage them from leaving, or show an overlay on your page and prevent the user from clicking links on the displayed page (at least until your ajax response arrives). Make sure they understand that leaving the page before their operation is complete may cause problems.

If I understand your question correctly, it sounds like you're firing off an ajax request, but then clicking on another link on a page, and so the ajax request never completes?

If that's the case you could bind a function to all links that would .preventDefault() and then when the AJAX request is complete, you could unbind that function.

Something along the lines of:

function makeAJAXRequest(){
    $('a').click(function(){});
    //Do AJAX Request Here
}
function AJAXCallbackk(){
    $('a').unbind('click');
    //Parse AJAX here
}

You could also bind a function to onBeforeUnload that would create a window.confirm() which could prompt the user to ask if they are sure you want to leave a page. Many websites do this if an input box has unsubmitted text. Facebook is an obvious example.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM