ajax 为什么我们将 null 传递给 XMLHttpRequest.send?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15123839/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 10:57:27  来源:igfitidea点击:

Why do we pass null to XMLHttpRequest.send?

javascriptajax

提问by Waleed Khan

Why is sendso often called as

为什么send经常被称为

xhr.send(null)

instead of

代替

xhr.send()

?

?

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

W3MDNMSDN都声明它是可选的。此外,ActiveX 控件似乎不需要参数

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:

谷歌地图的做法send(null)至少可以追溯到2005 年,但被缩小了,没有解释:

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

采纳答案by bfuoco

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

如果您看一下 XMLHttpRequest 的旧规范,似乎 W3C 似乎并不要求该参数在某一点是可选的,这可能导致人们“以防万一”提供显式空值。

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

(搜索“应该支持发送”) 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')

我遇到的另一个可能的原因来自俄语页面的翻译,可在此处查看:长谷歌翻译链接(搜索“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:

当你发送一个没有 ActiveX 的版本的 GET 请求时,你必须指定 null,否则你不能指定任何参数。如果始终将 GET 指定为 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'.

我不知道这是否属实,但如果 GET 参数包含在正文中,那么如果数据值为“未定义”,则正文可能不会生成,这似乎是合理的。

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

不幸的是,我在搜索中找不到任何更确定的信息。

回答by Kernel James

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

不添加null将在旧版本的 Firefox 中引发异常。

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

这种行为早在 2002 年就存在,并通过 Firefox 3 (2008) 存在

回答by theVoogie

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 gonna stay emptyand that is why you need to set the value of that argument to null.

因此,如果您的 HTTP 请求方法是GET,那么 HTTP 客户端只需将数据附加到请求 URL(作为所谓的查询字符串)就可以将数据发送到服务器,这意味着请求的正文将保持为,即为什么需要将该参数的值设置为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 sendfunction.

但是,如果您的 HTTP 请求方法是POST,那么您的请求数据将被放入请求正文中,最终将通过send函数发送到服务器。

Cheers!

干杯!

回答by Mukundhan

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.

XMLHttpRequest.send() 方法发送请求。如果请求是异步的(这是默认值),则该方法会在请求发送后立即返回。如果请求是同步的,则此方法在响应到达之前不会返回。send() 接受请求正文的可选参数。如果请求方法是 GET 或 HEAD,则忽略该参数并将请求正文设置为 null。