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.
Note: Be aware to stop using a plain ArrayBuffer
as parameter. This is not part of the XMLHttpRequest
specification any longer. Use an ArrayBufferView
instead (see compatibility table for version information).
void send();void send(ArrayBuffer data);void send(ArrayBufferView data); void send(Blob data); void send(Document data); void send(DOMString? data); void send(FormData data);
If the data is a Document
, it is serialized before being sent. When sending a Document, versions of Firefox prior to version 3 always sends the request using UTF-8 encoding; Firefox 3 properly sends the document using the encoding specified by body.xmlEncoding
, or UTF-8 if no encoding is specified.
If it's an nsIInputStream
, it must be compatible with nsIUploadChannel
's setUploadStream()
method. In that case, a Content-Length header is added to the request, with its value obtained using nsIInputStream
's available()
method. Any headers included at the top of the stream are treated as part of the message body. The stream's MIMEtype should be specified by setting the Content-Type header using the setRequestHeader()
method prior to calling send()
.
The best way to send binary content (like in files upload) is using an ArrayBufferView or Blobs in conjuncton with the send()
method.
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);
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);
Specification | Status | Comment |
---|---|---|
XMLHttpRequest The definition of 'send()' in that specification. | Living Standard | WHATWG living standard |
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
---|---|---|---|---|---|
Basic support | 1 | 1.0 (1.7 or earlier) | 5[2] 7 | (Yes) | 1.2 |
send(ArrayBuffer) | 9 | 9.0 (9.0)[1] | 10 | 11.60 | ? |
send(ArrayBufferView) | 22 | 20.0 (20.0) | ? | ? | ? |
send(Blob) | 7 | 3.6 (1.9.2) | 10 | 12 | ? |
send(FormData) | 6 | 4.0 (2.0) | 10 | 12 | ? |
Feature | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|
Basic support | ? | 1.0 | (Yes) | ? | ? | ? |
send(ArrayBuffer) | ? | ? | ? | ? | ? | |
send(ArrayBufferView) | ? | ? | ? | ? | ? | |
send(Blob) | ? | ? | ? | ? | ? | |
send(FormData) | ? | ? | ? | ? | ? |
[1] Sending a plain ArrayBuffer
is not part of the XMLHttpRequest
specification any longer and should be treated as deprecated. Use ArrayBufferView
instead, which was added to Gecko version 20.0 (Firefox 20.0 / Thunderbird 20.0 / SeaMonkey 2.17).
[2] This feature was implemented via ActiveXObject()
. Since version 7 Internet Explorer implements the standard XMLHttpRequest
.
© 2005–2017 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