I have Ajax request to update the client page if any new data is available to server.I have set the connection 'keep-Alive' So here I am not making new Ajax call every time to check the updated data. I have callback which update the page if any records is available.
Below is my Ajax Request.
xmlRequest.open("post", url, true);
xmlRequest.setRequestHeader("Connection", "Keep-Alive");
xmlRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
xmlRequest.send(some data);
It works fine, but if user refresh the browser then, Ajax does not get the updated data from server.
My concept is not very clear on how the connection type "keep-Alive" works. but it seems when user refresh the browser then Ajax connection lost to the server hence Ajax stopped listing.
I know i can fix this by making new call whenever browser refresh the page. but here i want to understand does it really Ajax keep-Alive lose the connection when browser refresh.
Let's first try to understand what does Keep-alive mean it means: the server won't close the Connection after fulfilling the request So hence your sent the requet to the server, and server sent the response back to your browser, now in that case the request will not closed the server will keep the connection open so for every new request you send to server same requet object will be used.
So when you refresh your page the client/browser lost the connection so I think the best solution is to promot your users do not refresh page like it happen when we making transactions.
If you save your connection object to Browser storage this can not be useable!
Thanks
References: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Keep-Alive
Keep Ajax alive
https://reqbin.com/req/javascript/4sa9kqvu/keep-alive-connection-example#:~:text=Keep%2DAlive%20Connection%20Example%20%5BJavaScript,considered%20persistent%20unless%20declared%20otherwise.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With