是否有可能为一个AJAX请求被读取响应完成之前? [英] Is it possible for an AJAX request to be read before the response is complete?

查看:118
本文介绍了是否有可能为一个AJAX请求被读取响应完成之前?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有需要一段时间来完成Ajax请求,但服务器不会输出沿途的一些内容。如果我在浏览器中简单地装载的要求,我可以看到页面加载缓慢,它可以随时停止。是否有可能在服务器关闭响应之前访问一个不完整的Ajax请求?

I have an ajax request that takes a while to complete, but the server does output some content along the way. If I load the request simply in the browser, I can see the page slowly loading and it can be stopped at any time. Is it possible to access an incomplete ajax request before the server closes the response?

推荐答案

要做到这一点的方法是在XHR对象监听readyState的。 当readyState的== 3则意味着新的内容已经到来,您可以访问它。该技术被称为彗星

The way to accomplish this is by listening on the readyState in the the xhr object. When readyState == 3 it means new content has arrived and you can access it. The technique is referred to as Comet.

但是请注意,不同的浏览器的行为不同位置,IE不会让你访问它<一个href="http://stackoverflow.com/questions/4158435/ie7-8-responsetext-in-readystate-3-not-available/4718513#4718513">See在这里,和WebKit的浏览器(Chrome浏览器/ Safari浏览器),使之成为可用之前的数据缓冲区2KB。占,虽然之后,你就可以听的变化,然后采取行动。

Note however that different browsers behave differently here, IE will not allow you to access it See Here and webkit browsers (Chrome/Safari) buffer 2KB of data before making it available. After accounting for that though, you can then listen for the change then act on it.

不幸的是,jQuery的目前不支持此开箱即用。您实现这个愿望在错误#8327 ,指出他们基本上回落到投票的readyState的,看它是否改变。他们有计划,也许做一些事情,今后,错误#9883 ,但不要屏住呼吸。

Unfortunately, jQuery does not currently support this out of the box. You can get around this as noted in Bug #8327, where they basically fall back to polling on the readyState to see if it changes. They have plans to maybe do something about it in the future, Bug #9883, but don't hold your breath.

所以最后,是有可能的,不,它是不容易的。

So finally, yes it is possible, no it is not easy.

这篇关于是否有可能为一个AJAX请求被读取响应完成之前?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆