试图在内部理解 AJAX 模型。 根据A typical HTTP session
In client-server protocols, like HTTP, sessions consist of three phases:
- The client establishes a TCP connection (or the appropriate connection if the transport layer is not TCP).
- The client sends its request, and waits for the answer.
- The server processes the request, sending back its answer, providing a status code and appropriate data.
这如何适合 AJAX 调用?
是否有与后台服务器的连接保持事件状态(在不同的线程中),以便浏览器不会被阻塞并可以做其他事情?
然后当服务器的结果就绪时,它会被推送到浏览器 通过这个“隐藏”连接,然后浏览器更新 DOM?
我的意思是,是否存在由 AJAX 维护的打开 session ,或者它是否因为 HTTP 协议(protocol)是无状态的而关闭和打开连接/ session ?
最佳答案
你说的是什么 session ?一个 http/2 应用程序通常会实现至少 4 个 session 层(但有些人仍然试图用 OSI 模型来描述它)。
在执行线程方面,页面上的 JavaScript 作为单线程运行。但是,如果 open 方法的第三个参数为真,则在调用 xmlhttprequest 对象上的发送时线程不会被阻塞。如果为 false,send() 将阻塞直到它得到响应。对于异步方法,您可以分配一个 Action ,以便稍后在请求对象触发事件时执行。 xmlhttprequest 和底层网络堆栈之间接口(interface)的低级处理表现得好像它是一个自主执行线程,但可能在 select()/poll() 操作中作为单线程实现——这是在浏览器内部实现的代码,因此该问题仅在您碰巧正在编写浏览器时才有意义。
关于javascript - 典型的 HTTP session ,但采用 Ajax 风格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50660364/