Jeff
Jeff

Reputation: 12428

POST doesn't complete if I refresh page

I have a form which uses $.ajax on submit to call a bash script that writes data to a text file.

In Firefox, if i immediately refresh the page or navigate to a different page, the POST aborts, and the text file does not update:

[15:07:12.781] POST http://server/writegit.cgi [undefined 891ms]

If I wait a second or two before refreshing, the POST completes and everything is fine:

[15:03:04.995] POST http://server/writegit.cgi [HTTP/1.1 200 OK 2022ms]

However, this problem does not happen in Chrome-- once the form submits, the POST will complete even if I refresh the page immediately.

Is there any way I can make Firefox behave like Chrome?

Upvotes: 0

Views: 211

Answers (1)

Diodeus - James MacFarlane
Diodeus - James MacFarlane

Reputation: 114417

Ajax calls are asynchronous - meaning they fire off and sometime in the future a response will come. The code does not wait for the response. If you navigate away from the page before this happens, you pull the rug out from under the request.

Either make the call synchronous, or add some "onbeforeunload" event handler to check if your ajax call has completed before allowing the user to navigate away from the page.

or: just don't refresh the page!

Upvotes: 4

Related Questions