【问题标题】:Is there any use in the send parameter for XMLHttpRequests type GET?XMLHttpRequests类型GET的发送参数有什么用吗?
【发布时间】:2017-04-19 05:39:23
【问题描述】:

我很好奇是否可以将此参数用于任何有用的事情。

这里的代码是:xhr.send(param)

var xhr;
if (config_ajax.type === 'get') {
    xhr = new win.XMLHttpRequest();
    xhr.open('GET', config_ajax.url, true);
    // .. snip
    xhr.send(null);
}

这是我检查的文档。

https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest

【问题讨论】:

    标签: javascript xmlhttprequest


    【解决方案1】:

    不,您无法对GET 请求中的参数执行任何操作。来自XMLHttpRequest.send()

    如果请求方法是 GET 或 HEAD,则忽略参数并将请求正文设置为 null。

    如果方法是POSTPUT,则使用该参数。

    你可以写xhr.send()或者xhr.send(null),没有区别。

    【讨论】:

    • 所以我应该将它显式设置为 null 吗?什么是最佳做法?
    • 我怀疑xhr.send() 是最常见的,但我没有证据。可能绝大多数 XHR 请求使用 POST,而不是 GET,除非使用 REST API。
    • 我使用 Ajax GET 来加载我的所有文件 b.c.我是疯子
    【解决方案2】:

    您可以通过作为参数发送数据。来自文档:

    The best way to send binary content (like in files upload) is using an ArrayBufferView or Blobs in conjuncton with the send() method.

    https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/send

    【讨论】:

    • 但不是 GET 请求。
    • 你链接到的网页说当method = GET时参数被忽略。
    • 我的错误 - 看起来它只是被忽略了。
    猜你喜欢
    • 2012-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-20
    • 1970-01-01
    相关资源
    最近更新 更多