The XMLHttpRequest.send()
method sends the request. If the request is asynchronous (which is the default), this method returns as soon as the request is sent. If the request is synchronous, this method doesn't return until the response has arrived. send()
accepts an optional argument for the request body. If the request method is GET
or HEAD
, the argument is ignored and request body is set to null.
If no Accept
header has been set using the setRequestHeader()
, an Accept
header with the */*
is sent.
Syntax
XMLHttpRequest.send(body)
Parameters
-
body Optional
-
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. - A
BodyInit
, which as per the Fetch spec can be aBlob
,BufferSource
,FormData
,URLSearchParams
,ReadableStream
, orUSVString
object.
- A
The best way to send binary content (e.g. in file uploads) is by using an ArrayBufferView or Blobs in conjunction with the send()
method.
If no value is specified for the body, a default value of null
is used.
Return value
Void.
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({ form: 'data' }); // 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(xhr.readyState == XMLHttpRequest.DONE && xhr.status == 200) { // Request finished. Do processing here. } } xhr.send("foo=bar&lorem=ipsum"); // xhr.send('string');
//xhr.send(new Blob()); // xhr.send(new Int8Array()); // xhr.send({ form: 'data' }); // xhr.send(document);
Specifications
Specification | Status | Comment |
---|---|---|
XMLHttpRequest The definition of 'send()' in that specification. |
Living Standard | WHATWG living standard |
Browser compatibility
Feature | Chrome | Edge | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
---|---|---|---|---|---|---|
Basic support | 1 | (Yes) | 1.0 (1.7 or earlier) | 5[1] 7 |
(Yes) | 1.2 |
send(ArrayBuffer) |
9 | (Yes) | 9.0 (9.0) | 10 | 11.60 | ? |
send(ArrayBufferView) |
22 | ? | 20.0 (20.0) | ? | ? | ? |
send(Blob) |
7 | (Yes) | 3.6 (1.9.2) | 10 | 12 | ? |
send(FormData) |
6 | (Yes) | 4.0 (2.0) | 10 | 12 | ? |
send(URLSearchParams) |
59 | ? | 44.0 (44.0) | ? | ? | ? |
Feature | Android Webview | Chrome for Android | Edge | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|---|
Basic support | ? | 1 | (Yes) | (Yes) | ? | ? | ? |
send(ArrayBuffer) |
? | ? | ? | ? | ? | ? | ? |
send(ArrayBufferView) |
? | ? | ? | ? | ? | ? | ? |
send(Blob) |
? | ? | ? | ? | ? | ? | ? |
send(FormData) |
? | ? | ? | ? | ? | ? | ? |
send(URLSearchParams) |
? | 59 | ? | 44.0 (44.0) | ? | ? | ? |
[1] This feature was implemented via ActiveXObject()
. Since version 7 Internet Explorer implements the standard XMLHttpRequest
.