Does Ajax connection disconnect at some point?

会有一股神秘感。 提交于 2019-12-11 07:29:18

问题


My Co-worker told me that AJAX connection alive until a user closes his/her browser. As far as I know, ajax connection closes its connection when its request has completed. I tested it with Firebug and a HTTP monitoring tool, I noticed that AJAX connection closes itself.

Is he correct????


回答1:


Break down what AJAX is -- and XMLHttpRequest. It's a connection to a URI endpoint for some resource (image, text, whatever). Your browser closes the HTTP connection as soon as it's done.




回答2:


Ajax is just like any other request, when it completes the connection is closed. Your colleague is wrong.

Note : There are connection types that allow you to keep the connection open indefinitely




回答3:


Ajax connections closed after receiving data or if you close tab, then connections will force closed.

Here described Ajax life cycle.




回答4:


Late to the party here, but this regards an issue I'm actually dealing with right now...

I have a web server running on a severely resource constrained platform (128k Flash, 48k RAM) so it can only handle one connection at a time. Further connections are not handled until the current one is closed. Also, it does not force Connection: close on certain URLs because of a low latency requirement. In general, only one thing talks to the device at a time.

AJAX connections follow whatever rules the browser sets for other connections. In my case, I'm testing a web page that uses AJAX to read one of the keep-alive-allowed URLs once per second, and the browser does not close the connection until several seconds after the window is closed. As a result, other clients wait indefinitely.

So don't assume that XHR connections are closed when complete. They might not be; Firefox 21 sure isn't closing them.

My current problem is that I want to force my AJAX requests to close the socket on completion, and I'm using jQuery's .ajaxSend() pre-send hook to set the Connection: close header. The AJAX seems to be working, but when another client tries to connect, it gets "connection reset by peer", so I'm wondering if Firefox doesn't notice the "Connection: close" header on the XHR request and keeps its end of the socket open (until it times out after approximately three seconds) even after the server has closed its side.




回答5:


Using JQuery to make AJAX requests, some connections are maintained until the following AJAX call. This can become a real problem when the server holds streams open until the response's close event fires.



来源:https://stackoverflow.com/questions/5164081/does-ajax-connection-disconnect-at-some-point

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!