Movatterモバイル変換


[0]ホーム

URL:


  1. Web
  2. Web APIs
  3. XMLHttpRequest
  4. send()

XMLHttpRequest: send() method

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since ⁨July 2015⁩.

Note: This feature is available inWeb Workers, except forService Workers.

TheXMLHttpRequest methodsend() sends the request to the server.

If therequest is asynchronous (which is the default), this method returns as soon as therequest 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'sbody; this is primarily used for requests such asPUT. If the requestmethod isGET orHEAD, thebodyparameter is ignored and the request body is set tonull.

If noAccept header has been set using thesetRequestHeader(), anAccept header with the type"*/*" (any type) is sent.

Syntax

js
send()send(body)

Parameters

bodyOptional

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 ofnull is used.

The best way to send binary content (e.g., in file uploads) is by usingaTypedArray, aDataView or aBlob objectin conjunction with thesend() method.

Return value

None (undefined).

Exceptions

InvalidStateErrorDOMException

Thrown ifsend() 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 notGET.

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 requestxhr.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

Browser compatibility

See also

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp