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.
XMLHttpRequest.send(body)
body
Optional
Document
, in which case it is serialized before being sent.BodyInit
, which as per the Fetch spec can be a Blob
, BufferSource
, FormData
, URLSearchParams
, ReadableStream
, or USVString
object.null
is used.The best way to send binary content (e.g. in file uploads) is by using an ArrayBufferView
or Blob
in conjunction with the send()
method.
undefined
.
Exception | Description |
---|---|
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 . |
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);
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);
Specification | Status | Comment |
---|---|---|
XMLHttpRequest The definition of 'send()' in that specification. | Living Standard | WHATWG living standard |
Desktop | ||||||
---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | |
Basic support | 1 | 12 | 1 | 7
|
Yes | 1.2 |
ArrayBuffer as parameter to send() | 9 | Yes | 9 | 10 | 11.6 | Yes |
ArrayBufferView as parameter to send() | 22 | ? | 20 | ? | Yes | ? |
Blob as parameter to send() | 22 | Yes | 2 | 10 | 12 | ? |
FormData as parameter to send() | 6 | Yes | 2 | 10 | 12 | ? |
URLSearchParams as parameter to send() | 59 | ? | 44 | ? | 12 | ? |
Mobile | |||||||
---|---|---|---|---|---|---|---|
Android webview | Chrome for Android | Edge Mobile | Firefox for Android | Opera for Android | iOS Safari | Samsung Internet | |
Basic support | Yes | 18 | Yes | 4 | Yes | Yes | Yes |
ArrayBuffer as parameter to send() | Yes | Yes | ? | 9 | Yes | ? | Yes |
ArrayBufferView as parameter to send() | Yes | Yes | ? | 20 | Yes | ? | Yes |
Blob as parameter to send() | Yes | Yes | ? | 4 | Yes | ? | Yes |
FormData as parameter to send() | Yes | Yes | ? | 4 | Yes | ? | Yes |
URLSearchParams as parameter to send() | Yes | 59 | ? | 44 | Yes | ? | 7.0 |
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/send