admin管理员组

文章数量:1355522

Multipart/x-mixed-replace is a MIME-type for content with multiple parts, each replacing the previous part. This can be used to implement server push / reverse ajax / et, and apparently should work at least in Firefox. To test this out, I have set up a server which produces the following output with delay between each part:

HTTP/1.1 200 OK
Content-type: multipart/x-mixed-replace; boundary=whatever

--whatever
Content-type: text/plain

tick

--whatever
Content-type: text/plain

tock

--whatever
...

On the client side, this is the JavaScript code which I run in Firefox:

var r = new XMLHttpRequest();
r.multipart = true;
r.open('GET', '/', true);
r.onreadystatechange = function () {
    console.log(r.responseText.length);
};
r.send();

I expected each responseText to replace the previous one, but it seems they are actually appended together. The size of the responseText keeps increasing as the server produces more output. Is there a way to only get the latest replaced part?

Multipart/x-mixed-replace is a MIME-type for content with multiple parts, each replacing the previous part. This can be used to implement server push / reverse ajax / et, and apparently should work at least in Firefox. To test this out, I have set up a server which produces the following output with delay between each part:

HTTP/1.1 200 OK
Content-type: multipart/x-mixed-replace; boundary=whatever

--whatever
Content-type: text/plain

tick

--whatever
Content-type: text/plain

tock

--whatever
...

On the client side, this is the JavaScript code which I run in Firefox:

var r = new XMLHttpRequest();
r.multipart = true;
r.open('GET', '/', true);
r.onreadystatechange = function () {
    console.log(r.responseText.length);
};
r.send();

I expected each responseText to replace the previous one, but it seems they are actually appended together. The size of the responseText keeps increasing as the server produces more output. Is there a way to only get the latest replaced part?

Share Improve this question asked Dec 2, 2013 at 2:02 BemmuBemmu 18.3k16 gold badges81 silver badges94 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

This is no longer possible, as the support was removed from Firefox. See https://bugzilla.mozilla/show_bug.cgi?id=843508

How about storing the response length after each part, and using that as the offset:

var offset = 0;
var r = new XMLHttpRequest();
r.multipart = true;
r.open('GET', '/', true);
r.onreadystatechange = function () {
    var latestPart = r.responseText.substring(offset) 
    offset = r.responseText.length;
};
r.send();

本文标签: javascriptUsing multipartxmixedreplace with XMLHttpRequestStack Overflow