0

I am having a website where a user submits a form using ajax. The response is shown in alert whether the form was successfully submitted or the details of the problem. Now since this process is asynchronous, if the user navigates to another page before the response is received, the XMLHttpRequest object that was used for submitting the form is deleted and the user does not get feedback about the status of the submitted form.

So, is it possible to have the XMLHttpRequest object remain active even if the user navigates to another page?

Blip
  • 3,061
  • 5
  • 22
  • 50
  • Why don't you utilize cookies? That's a better solution. – Mike Jul 09 '15 at 14:05
  • Or local/session storage. – Cerbrus Jul 09 '15 at 14:06
  • 2
    Or opaque the page until a response is received. – Dean.DePue Jul 09 '15 at 14:08
  • 1
    I don't see how either cookies or local storage would help at all here. – Pointy Jul 09 '15 at 14:08
  • neither of the first two suggestions would make a difference if the user navigates away from the page before there's a response from the server – Jaromanda X Jul 09 '15 at 14:09
  • Yeah, cookies and local storage won't fix anything - the HTTP request will abort as soon as the page unloads. You'd have to implement something like PJAX so page navigation is loaded via AJAX instead of individual pageviews. – ceejayoz Jul 09 '15 at 14:09
  • 2
    That duplicate post does not solve the problem and is merely minor related to it. – JavaScript Jul 09 '15 at 14:11
  • @ceejayoz, doesn't it actually go the other way around? I mean, the page will wait for XHR to finish before it unloads. I'm not sure but that's what I always thought. – matewka Jul 09 '15 at 14:13
  • Usually you get a status 0 response when the XHR gets cancelled in between. Not sure if that is supposed to be but it is my experience. – JavaScript Jul 09 '15 at 14:14
  • @matewka If the user clicks a link or closes the page, the XHR will not finish. Sites like Facebook/Twitter load all in-site navigation via AJAX for this reason. – ceejayoz Jul 09 '15 at 14:24
  • @Cerbrus I did not find my solution in the post you marked duplicate. Could you also tell me how I can use local/session storage in my case? – Blip Jul 09 '15 at 14:31
  • Oh, apparently the title of the OP doesn't describe the problem of the OP. If someone has a better dupe, feel free to change it. – Cerbrus Jul 09 '15 at 14:35
  • @Cerbrus I have corrected the title to reflect my problem. Could you elaborate about your suggestion on local/session storage? – Blip Jul 09 '15 at 14:46
  • 1
    Local / session storage won't help if you navigate away from the page before the request finished loading. – Cerbrus Jul 09 '15 at 14:47
  • @Dean.DePue I have gone with your suggestion of **Opeque the page** and showed a simple animation till the response is received. – Blip Jul 14 '15 at 15:43

1 Answers1

1

So, is it possible to have the XMLHttpRequest object remain active even if the user navigates to another page?

No, it's not. The XMLHttpRequest is part of the previous page's context, and disappears along with it.

The best solution is to somehow prevent the user from navigating away from the page until the request is finished. The unload event might come in handy here.

  • 1
    `onbeforeunload` would probably be a better choice - the user might have legitimate reasons for closing the window or navigating away. – ceejayoz Jul 09 '15 at 15:44