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.
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.
- Possible duplicate of HTTP: what are the relations between pipelining, keep-alive and Server Sent Events? – bishop Commented Jul 14, 2019 at 1:08
2 Answers
Reset to default 1Let'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/en-US/docs/Web/HTTP/Headers/Keep-Alive
Keep Ajax alive
https://reqbin./req/javascript/4sa9kqvu/keep-alive-connection-example#:~:text=Keep%2DAlive%20Connection%20Example%20%5BJavaScript,considered%20persistent%20unless%20declared%20otherwise.
The javascript environment normally gets cleared on page reset, but you can use window.localStorage
to persist across refresh and maintain your xmlRequest variable. You should then be able to test client + server side together to see if it is necessary to re-initialize the connection.