Correct way to make an AJAX call that doesn't need a response?

ajax, javascript

Solution

You need to keep the connection open until the server is done with it, regardless of whether or not you expect (or will be or won't be) a reply from the server.

The reason is that most webservers will kill requests if the client disconnects before the script has finished executing.

The server doesn't have to "respond" as in return data, it just needs to "return".

i.e. in answer to your question "Does Ajax connection disconnect at some point?": Yes, when the server terminates the connection.

So what you need to do is make sure that your ASP(X) script terminates as soon as it has finished its work. Don't let it run forever, pass data to a helper daemon/service, etc. that will need long-running processing.

Problem

Is there a proper way to make an AJAX call when I don't want a response back from the server? I want the server to save some data, but the client doesn't need a response. If the server never responds, will the AJAX connection remain open, waiting for a response until it times out? I would use Javascript like the following. It's a typical AJAX call except I left out the xmlhttp.onreadystatechange event. ``` function SaveLabel() { var xmlhttp = null; if (window.XMLHttpRequest) { xmlhttp = new XMLHttpRequest(); } else if (window.ActiveXObject) { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } else { alert('Your browser does not support this feature.'); return; } xmlhttp.open('GET', 'mypage.aspx?label=1'); xmlhttp.send(null); } ``` I found a similar question, but it doesn't really answer mine: Does Ajax connection disconnect at some point?

Original source