jQuery 以增量方式读取 AJAX 流? [英] jQuery read AJAX stream incrementally?

查看:30
本文介绍了jQuery 以增量方式读取 AJAX 流?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了这个问题,但它没有完全回答我的问题.不幸的是,自从我上次查看 AJAX 以来,XHR 对象中的情况似乎发生了变化,因此在完成填充之前不再可能直接访问 responseText.

I have read this question but it doesn't exactly answer my question. Unfortunately, it looks like things have changed in in the XHR object since I last looked at AJAX, so it is no longer possible to directly access responseText before it is finished being populated.

我必须编写一个页面,该页面使用 AJAX(最好是 jQuery,但我愿意接受建议)通过 HTTP 从我无法控制的服务器检索 CSV 数据.响应数据可能非常大;一兆字节的文本并不少见.

I have to write a page that uses AJAX (preferably jQuery, but I am open to suggestions) to retrieve CSV data via HTTP from a server I have no control over. The response data could be quite large; a megabyte of text is not uncommon.

服务器对流友好.是否还有任何方法可以直接从 JavaScript 访问正在返回的数据流?

The server is stream-friendly. Is there still any way to get access to a stream of data as it is being returned, directly from JavaScript?

我确实可以选择编写一些位于中间并使用某种彗星"技术(长轮询、EventSource 等)的 PHP 代码,但如果可能的话,我宁愿避免这种情况.

I do have the option of writing some PHP code that lives in the middle and uses some sort of "Comet" tech (long-polling, EventSource, etc), but I would prefer to avoid that if possible.

如果相关,请假设用户拥有最新版本的 Firefox/Chrome/Opera 并且旧浏览器兼容性不是问题.

推荐答案

为此,您将需要直接使用 javascript.原因是您将希望持续轮询而不是等待回调触发.为此,您不需要 jQuery,它非常简单.他们在 Ajax 模式网站上有一些 很好的源代码.

You're going to want to use straight up javascript for this. The reason is that you're going to want to continuously poll and not wait for the callbacks to fire. You don't need jQuery for this, it's pretty simple. They have some nice source code for this on the Ajax Patterns website.

本质上,您只想跟踪您在响应中的最后一个位置,并定期轮询超过该位置的更多文本.您的情况的不同之处在于您可以订阅完整事件并停止投票.

Essentially, you'll just want to keep track of your last position in the response and periodically poll for more text past that location. The difference in your case is that you can subscribe to the complete event and stop your polling.

这篇关于jQuery 以增量方式读取 AJAX 流?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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