为什么我的 Ajax 请求直接从状态 1 转到状态 4?

发布于 2024-07-10 07:27:07 字数 150 浏览 3 评论 0原文

我正在使用 AJAX 向 CGI 程序发出请求。 响应向我发送内容长度。 我的目的是动态展示响应进度。 为此,我需要在 XHR 对象的 onreadystate 值为 3 的情况下启动一个函数。但请求似乎并未获取该状态号。 相反,它直接从状态 1 进入状态 4。 我缺少什么?

I am making a request to a CGI program using AJAX. The response sends me content-length. My purpose is to dynamically exhibit the response progress. For that I need to start a function on onreadystate value of XHR object to be 3. But the request doesn't seems to acquire that status number. Instead it goes directly from state 1 to state 4.
What am I missing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

他不在意 2024-07-17 07:27:07

响应可能进行得如此之快,以至于您在状态 3 时根本没有注意到它。特别是如果您在本地主机上运行它,则响应可能会传输得非常快。 您可以尝试在到达第 3 阶段时设置警报,以测试它是否真正到达该阶段。 另外,我相信 Internet Explorer 表示在第 3 阶段访问响应是错误的,因此可能存在兼容性问题。

The response could be going so quickly that you just don't notice it at state 3. Especially if you are running it on localhost, the response could be transmitted very quickly. You could try setting an alert when it gets to stage 3 to test whether it's actually getting there. Also, I belive internet explorer says that it is a mistake to access the response in stage 3 so there could be compatibility issues.

可是我不能没有你 2024-07-17 07:27:07

如果您在本地主机上运行,​​那么浏览器可能永远没有机会在发送请求的时间和获得响应的时间之间运行...

  1. 浏览器打开连接,将readyState设置为1
  2. 浏览器将数据包发送到服务器进程
  3. 服务器进程接收数据包,从调度程序获取优先级
  4. 服务器将数据返回到浏览器,并让出对CPU的控制权。 浏览器继续执行。
  5. 浏览器看到所有数据都已收到,将readyState设置为4。

长话短说:不要指望进入“接收”状态。

If you're running on localhost, then probably the browser is never getting a chance to run between the time it sends the request and the time it gets the response...

  1. browser opens connection, sets readyState to 1
  2. browser sends packet to server process
  3. server process receives packet, gets priority from scheduler
  4. server returns data to browser, and yields control of the CPU. Browser continues execution.
  5. browser sees all data has been received, sets readyState to 4.

Long story short: don't count on going into the "receiving" state.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文