XMLHttpRequest 方法 send() sends the request to the server. If the request is asynchronous (which is the default), this method returns as soon as the request is sent and the result is delivered using events. If the request is synchronous, this method doesn't return until the response has arrived.

send() accepts an optional parameter which lets you specify the request's body; this is primarily used for requests such as PUT . If the request method is GET or HEAD body parameter is ignored and the request body is set to null .

若无 Accept header has been set using the setRequestHeader() Accept header with the type "*/*" (any type) is sent.

句法

XMLHttpRequest.send(body)
					

参数

body 可选
A body of data to be sent in the XHR request. This can be: If no value is specified for the body, a default value of null 被使用。

The best way to send binary content (e.g. in file uploads) is by using an ArrayBufferView or Blob in conjunction with the send() 方法。

返回值

undefined .

异常

异常 描述
InvalidStateError send() has already been invoked for the request, and/or the request is complete.
NetworkError The resource type to be fetched is a Blob, and the method is not GET .

Example: GET

var xhr = new XMLHttpRequest();
xhr.open('GET', '/server', true);
xhr.onload = function () {
  // Request finished. Do processing here.
};
xhr.send(null);
// xhr.send('string');
// xhr.send(new Blob());
// xhr.send(new Int8Array());
// xhr.send(document);
					

Example: POST

var xhr = new XMLHttpRequest();
xhr.open("POST", '/server', true);
//Send the proper header information along with the request
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.onreadystatechange = function() { // Call a function when the state changes.
    if (this.readyState === XMLHttpRequest.DONE && this.status === 200) {
        // Request finished. Do processing here.
    }
}
xhr.send("foo=bar&lorem=ipsum");
// xhr.send(new Int8Array());
// xhr.send(document);
					

规范

规范 状态 注释
XMLHttpRequest
The definition of 'send()' in that specification.
实时标准 WHATWG (Web 超文本应用程序技术工作组) 实时标准

浏览器兼容性

The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
send Chrome 1 Edge 12 Firefox 1 IE 5 Opera 8 Safari 1.2 WebView Android 1 Chrome Android 18 Firefox Android 4 Opera Android 10.1 Safari iOS 1 Samsung Internet Android 1.0
ArrayBuffer as parameter to send() Chrome 9 Edge 12 Firefox 9 IE 10 Opera 11.6 Safari Yes WebView Android Yes Chrome Android Yes Firefox Android 9 Opera Android Yes Safari iOS ? Samsung Internet Android Yes
ArrayBufferView as parameter to send() Chrome 22 Edge ≤79 Firefox 20 IE ? Opera Yes Safari ? WebView Android Yes Chrome Android Yes Firefox Android 20 Opera Android Yes Safari iOS ? Samsung Internet Android Yes
Blob as parameter to send() Chrome 22 Edge 12 Firefox 2 IE 10 Opera 12 Safari ? WebView Android Yes Chrome Android Yes Firefox Android 4 Opera Android Yes Safari iOS ? Samsung Internet Android Yes
FormData as parameter to send() Chrome 6 Edge 12 Firefox 2 IE 10 Opera 12 Safari ? WebView Android Yes Chrome Android Yes Firefox Android 4 Opera Android Yes Safari iOS ? Samsung Internet Android Yes
URLSearchParams as parameter to send() Chrome 59 Edge ≤79 Firefox 44 IE ? Opera 12 Safari ? WebView Android Yes Chrome Android 59 Firefox Android 44 Opera Android Yes Safari iOS ? Samsung Internet Android 7.0

图例

完整支持

完整支持

兼容性未知 ?

兼容性未知

另请参阅

元数据

  • 最后修改: