Streaming JSON fragments to jquery
Is it possible to use jquery to hold open an HTTP connection and stream in data?
From my web server, I get the following (each JSON object is separated by a newline)
HTTP/1.1 200 OK
Content-Type: text/plain
Transfer-Encoding: chunked
{"a":{"uptime":15876}}
{"a":{"uptime":15877}}
{"a":{"uptime":15878}}
{"a":{"uptime":15879}}
...
In my web page I'm doing:
$.ajax({
type: "GET",
url: 'http://server/stream',
data: function(data) { console.log("data="+data); },
timeout: 20000,
dataType: "text",
error: function(XMLHttpRequest, textStatus, errorThrown) { console.log(textStatus); },
success: function(data) {console.log("done"+data); },
cache: false
});
I see no output and Firebug claims that there was no response to the HTTP request, I see a spinner in the Firefox tab.
tcpdump
shows that data is being received by the browser, but I never see any console logs, almost as if it's all being buffered away for printing on completion.
What am I missing?
What you are trying to do is considered long polling. It's not readily achievable with jquery, what you may want to look at, and this might be overkill, is a non-blocking server tech like node.js with that you could use this plugin with jQuery.
Not really. The browser doesn't hand the data over to jquery until the connection closes, so it's not going to work the way you want.
You need to use something like socket.io or the sadly neglected hookbox to manage a websocket connection for you.
链接地址: http://www.djcxy.com/p/7580.html上一篇: WPF将与Windows 8相关吗?
下一篇: 将JSON片段流式传输到jQuery