31

Why is send so often called as

xhr.send(null)

instead of

xhr.send()

?

W3, MDN, and MSDN all state that it's optional. Furthermore, the ActiveX control doesn't seem to need the argument:

hr=pIXMLHTTPRequest.CreateInstance("Msxml2.XMLHTTP.6.0");
SUCCEEDED(hr) ? 0 : throw hr;

hr=pIXMLHTTPRequest->open("GET", "http://localhost/books.xml ", false);
SUCCEEDED(hr) ? 0 : throw hr;

hr=pIXMLHTTPRequest->send(); // <-- this line
SUCCEEDED(hr) ? 0 : throw hr;

The practice of send(null) goes back at least as far as 2005 in Google Maps, but being minified, there's no explanation:

Y.asynchronousTransform = function (qc, vb, kc, Nc, Ba) {
    if (m.type == 3) return;
    var cc = Y.getCached(kc);
    if (cc) {
        cc.transformToHTML(qc, vb);
        if (Nc) Nc();
        return
    }
    var yc = qa.create(Ba);
    var sa = Xd.create();
    nd('<a href="' + kc.xmlEscape() + '">' + kc.xmlEscape() + "</a>", 0);
    sa.open("GET", kc, true);
    sa.onreadystatechange = function () {
        if (sa.readyState == 4) {
            if (yc.isValid()) {
                try {
                    var Db = sa.responseXML;
                    var cc = Y.create(Db);
                    Y.cache(kc, cc);
                    cc.transformToHTML(qc, vb);
                    if (Nc) Nc()
                } catch (b) {}
            }
        }
    };
    sa.send(null)
}
Waleed Khan
  • 11,426
  • 6
  • 39
  • 70
  • 2
    It's just explicitly saying that nothing is being sent to the server after the request headers. – Paul S. Feb 27 '13 at 23:14
  • @PaulS *Why* is it explicitly saying so? Nobody would get confused, and it *is* valid syntax. – Waleed Khan Feb 27 '13 at 23:16
  • 1
    Why do people do `while (i > 0)` when they could do `while (i)`? It's just a style. – Paul S. Feb 27 '13 at 23:19
  • 5
    @PaulS ...because `i` could be negative? – Waleed Khan Feb 27 '13 at 23:20
  • 1
    Did you try Googling how to make an Ajax request, every example has .send(null) and I guess old habits die hard, probably fits under this mantra "Don't fix it if it isn't broken" – Dave Mackintosh Feb 28 '13 at 12:37
  • 1
    @DaveMackintosh I did, but it doesn't appear `null` was **ever necessary**. – Waleed Khan Feb 28 '13 at 12:40
  • I wasn't saying it is/was, I'm just saying we've all learned from the same pool of information and haven't dropped the habit. I don't *think* there's a technical reason why lol. – Dave Mackintosh Feb 28 '13 at 12:41
  • The only reasoning I can think of is in the case where null and empty string can mean different things. Empty string is said to be the default if you do not explicitly enter a value. – jholloman Feb 28 '13 at 19:38

4 Answers4

14

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.

bfuoco
  • 715
  • 4
  • 12
8

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).

Kernel James
  • 3,752
  • 25
  • 32
4

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!

syntax-punk
  • 3,736
  • 3
  • 25
  • 33
1

The XMLHttpRequest.send() method sends the request. If the request is asynchronous (which is the default), this method returns as soon as the request is sent. If the request is synchronous, this method doesn't return until the response has arrived. send() accepts an optional argument for the request body. If the request method is GET or HEAD, the argument is ignored and request body is set to null.

Mukundhan
  • 3,284
  • 23
  • 36
  • 1
    Please add a link to the documentation from where you copied the above text, and use quote formatting too. – juzraai Sep 20 '17 at 13:58