XMLHttpRequest: send() method
The XMLHttpRequest method 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, the body parameter is ignored and the request body is set to null.
If no Accept header has been set using the setRequestHeader(), an Accept header with the type "*/*" (any type) is sent.
Syntax
Parameters
body Optional
-
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 is used.
The best way to send binary content (e.g. in file uploads) is by using a TypedArray, a DataView or a Blob object in conjunction with the send() method.
Return value
Exceptions
InvalidStateError DOMException
-
Thrown if send() has already been invoked for the request, and/or the request is complete.
NetworkError DOMException
-
Thrown if the resource type to be fetched is a Blob, and the method is not GET.
Example: GET
const xhr = new XMLHttpRequest();
xhr.open("GET", "/server", true);
xhr.onload = () => {
};
xhr.send(null);
Example: POST
const xhr = new XMLHttpRequest();
xhr.open("POST", "/server", true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.onreadystatechange = () => {
if (xhr.readyState === XMLHttpRequest.DONE && xhr.status === 200) {
}
};
xhr.send("foo=bar&lorem=ipsum");
Specifications
Browser compatibility
|
Desktop |
Mobile |
|
Chrome |
Edge |
Firefox |
Internet Explorer |
Opera |
Safari |
WebView Android |
Chrome Android |
Firefox for Android |
Opera Android |
Safari on IOS |
Samsung Internet |
ArrayBuffer |
9 |
12 |
9 |
10 |
11.6 |
6 |
≤37 |
18 |
9 |
12 |
6 |
1.0 |
ArrayBufferView |
22 |
12 |
20 |
10 |
11.6 |
7 |
4.4 |
25 |
20 |
12 |
7 |
1.5 |
Blob |
22 |
12 |
2 |
10 |
12 |
6 |
4.4 |
25 |
4 |
12 |
6 |
1.5 |
FormData |
6 |
12 |
2 |
10 |
12 |
6 |
≤37 |
18 |
4 |
12 |
6 |
1.0 |
URLSearchParams |
59 |
17 |
44 |
No |
46 |
15
10.1–15Doesn't send the correct Content-Type header by default. See bug 227477.
|
59 |
59 |
44 |
43 |
15
10.3–15Doesn't send the correct Content-Type header by default. See bug 227477.
|
7.0 |
send |
1 |
12 |
1 |
5 |
8 |
1.2 |
4.4 |
18 |
4 |
10.1 |
1 |
1.0 |
See also