Wednesday, February 17, 2010

Large Response in AJAX XMLHttpRequest

Sometimes happens that you need to send large data in the response of the XMLHttpRequest (XHR). For example you need to fetch a sorce code and display it somewhere in the document - exactly as I needed.

The code was as follows:

if (window.XMLHttpRequest) {
xhr=new XMLHttpRequest();
else {
xhr=new ActiveXObject("Microsoft.XMLHTTP");


// a bug is somewhere here:

Unfortunately this worked fine in Internet Explorer (IE). In FireFox (FF) it worked if the size of the code was less than certain limit. The limit in my FF 3.0 was 4096 bytes.

So the solution which works ion both IE and FF was the following:

cnt = xmlDoc.getElementsByTagName('code')[0].childNodes;

code = cnt[0].nodeValue;
for (i=1; i < cnt.length; i++) code += cnt[j].nodeValue;

So you do not need to create chunks on the server, just concatenate what FF split on the client.

No comments:

Post a Comment