简体   繁体   English

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

[英]Why do we pass null to XMLHttpRequest.send?

Why is send so often called as为什么send经常被称为

xhr.send(null)

instead of代替

xhr.send()

? ?

W3 , MDN , and MSDN all state that it's optional. W3MDNMSDN都声明它是可选的。 Furthermore, the ActiveX control doesn't seem to need the argument :此外,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 年的 Google Maps ,但是被缩小了,没有解释:

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

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.当你发送一个没有 ActiveX 的版本的 GET 请求时,你必须指定 null,否则你不能指定任何参数。 Will not fail if GET is always specified 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.不幸的是,我在搜索中找不到任何更确定的信息。

Not adding the null would throw an exception in older versions of Firefox.不添加null会在旧版本的 Firefox 中引发异常。

This behavior existed as early as 2002 and existed through Firefox 3 (2008) .这种行为早在 2002 年就存在,并且通过 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 .因此,如果您的 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 send function.但是,如果您的 HTTP 请求方法是POST ,那么您的请求数据将被放入请求正文中,最终将通过send函数发送到服务器。

Cheers!干杯!

The XMLHttpRequest.send() method sends the request. XMLHttpRequest.send() 方法发送请求。 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. send() 接受请求正文的可选参数。 If the request method is GET or HEAD, the argument is ignored and request body is set to null.如果请求方法是 GET 或 HEAD,则忽略该参数并将请求正文设置为 null。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM