最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Ajax Request header keep alive - Stack Overflow

programmeradmin1浏览0评论

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.

Share Improve this question edited Oct 11, 2013 at 14:37 user1591156 asked Oct 11, 2013 at 14:28 user1591156user1591156 2,0156 gold badges23 silver badges40 bronze badges 1
  • Possible duplicate of HTTP: what are the relations between pipelining, keep-alive and Server Sent Events? – bishop Commented Jul 14, 2019 at 1:08
Add a ment  | 

2 Answers 2

Reset to default 1

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/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.

发布评论

评论列表(0)

  1. 暂无评论