XMLHttpRequest: send() method - Web APIs | MDN
Syntax
Parameters
bodyOptional-
A body of data to be sent in the XHR request. This can be:
- A
Document, in which case it is serialized before being sent. - An
XMLHttpRequestBodyInit, which per the Fetch spec can be aBlob, anArrayBuffer, aTypedArray, aDataView, aFormData, aURLSearchParams, or a string. null
If no value is specified for the body, a default value of
nullis used. - A
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
None (undefined).
Exceptions
InvalidStateErrorDOMException-
Thrown if
send()has already been invoked for the request, and/or the request is complete. NetworkErrorDOMException-
Thrown if the resource type to be fetched is a Blob, and the method is not
GET.
Example: GET
js
const xhr = new XMLHttpRequest();
xhr.open("GET", "/server", true);
xhr.onload = () => {
// Request finished. Do processing here.
};
xhr.send(null);
// xhr.send('string');
// xhr.send(new Blob());
// xhr.send(new Int8Array());
// xhr.send(document);
Example: POST
js
const 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 = () => {
// Call a function when the state changes.
if (xhr.readyState === XMLHttpRequest.DONE && xhr.status === 200) {
// Request finished. Do processing here.
}
};
xhr.send("foo=bar&lorem=ipsum");
// xhr.send(new Int8Array());
// xhr.send(document);
Specifications
| Specification |
|---|
| XMLHttpRequest # the-send()-method |