Question

Is there a documented max to the length of the string data you can use in the send method of an XMLHttpRequest for the major browser implementations?

I am running into an issue with a JavaScript XMLHttpRequest Post failing in FireFox 3 when the data is over approx 3k. I was assuming the Post would behave the same as a conventional Form Post.

The W3C docs mention the data param of the send method is a DOMString but I am not sure how the major browsers implement that.

Here is a simplified version of my JavaScript, if bigText is over about 3k it fails, otherwise it works...

var xhReq = createXMLHttpRequest();

function createXMLHttpRequest() {
  try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {}
  try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {}
  try { return new XMLHttpRequest(); } catch(e) {}
  alert("XMLHttpRequest not supported");
  return null;
}

function mySubmit(id, bigText) {
  var url    = "SubmitPost.cfm";
  var params = "id=" + id + "&bigtext=" + encodeURI(bigText);

  xhReq.open("POST", url, true);

  //Send the header information along with the request
  xhReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  xhReq.setRequestHeader("Content-length", params.length);
  xhReq.setRequestHeader("Connection", "close");

  xhReq.onreadystatechange = onPostSubmit;
  xhReq.send(params);
}

function onPostSubmit() {

  if (xhReq.readyState==4 || xhReq.readyState=="complete")
     { 
     if (xhReq.status != 200)
        {
        alert('BadStatus');
        return;
        }
    } 
}
Was it helpful?

Solution

I believe the maximum length depends not only on the browser, but also on the web server. For example, the Apache HTTP server has a LimitRequestBody directive which allows anywhere from 0 bytes to 2GB worth of data.

OTHER TIPS

According to the XMLRPC spec, the only real limits are on the size of integers and doubles.

You don't specify how it fails however your encoding is incorrect. You should use encodeURIComponent not encodeURI.

Default the maximum size of the request entity body on the client is likely only limited by the available memory. The server as has already been pointed out may reject entity bodies over a certain size. IIS 6 and 7 for example have a 200KB default limit.

The configuration for Nginx must be done at the client_max_body_size and ca be set to any value i.e. 20m for 20MiB or set to 0 to disable it.

vim /etc/nginx/nginx.conf

Save and close the file, apply the changes to nginx.conf, then test -t and and send signal -s to reload:

/usr/sbin/nginx -t
/usr/sbin/nginx -s reload

Syntax: client_max_body_size size;

Default: client_max_body_size 1m;

Context: http, server, location

Sets the maximum allowed size of the client request body, specified in the “Content-Length” request header field. If the size in a request exceeds the configured value, the 413 (Request Entity Too Large) error is returned to the client. Please be aware that browsers cannot correctly display this error. Setting size to 0 disables checking of client request body size.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top