Why do we pass null to XMLHttpRequest.send?

Not adding the null would throw an exception in older versions of Firefox.

This behavior existed as early as 2002 and existed through Firefox 3 (2008).


So, if your HTTP request method is GET, then the HTTP client sends data to the server simply by appending it to the request URL (as a so called query string), meaning that the body of the request is going to be empty and that's why you need to set the value of that argument to null.

However, if your HTTP request method is POST, then your request data will be placed into the request body, which will be eventually sent to the server via send function.

Cheers!


If you'll take a look at an old specification of XMLHttpRequest, it seems like as though the W3C did not require that the parameter be optional at one point, which may have led to people supplying an explicit null value 'just in case'.

(search for 'SHOULD support the send') http://web.archive.org/web/20060409155734/http://www.w3.org/TR/XMLHttpRequest/

Another plausible reason I've come across comes from a translation of a russian page, viewable here: long Google Translate link (search for 'GET-Request for Version without ActiveX')

When you send a GET-request for version without ActiveX, you must specify null, otherwise you can not specify any parameters. Will not fail if GET is always specified null:

I have no idea if this is true or not but it seems plausible that if the GET parameters were included in the body, that the body may not have been generated if the data value was 'undefined'.

Unfortunately, I was unable to find anything more conclusive in my search.