aboutsummaryrefslogtreecommitdiff
path: root/files/fr/web/api/xmlhttprequest/response/index.md
blob: d07b064c0662b6984be9fd864e80f4d77d91f38d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
---
title: XMLHttpRequest.response
slug: Web/API/XMLHttpRequest/response
tags:
  - AJAX
  - Reference
  - XMLHttpRequest
translation_of: Web/API/XMLHttpRequest/response
---
{{draft}}{{APIRef('XMLHttpRequest')}}La propriété `XMLHttpRequest.response` contient le corps de la réponse. Elle peut être de type ArrayBuffer, Blob, Document, un objet JavaScript ou une DOMString en fonction de la valeur de la propriété `XMLHttpRequest.responseType`. La réponse ( `Value of response` ) est nulle si la requête est incomplète ou n'as pas été effectué avec succès. Cependant, si `responseType` est "text" ou une chaine vide et tant que la requête est en cours ( dans l'état _loading_ ), `response` peut contenir la réponse partielle.

<table class="standard-table">
  <tbody>
    <tr>
      <td class="header">Valeur de <code>responseType</code></td>
      <td class="header">
        Type de donnée de la propriété <code>response</code>
      </td>
    </tr>
    <tr>
      <td><code>""</code></td>
      <td>{{domxref("DOMString")}} (valeur par défaut)</td>
    </tr>
    <tr>
      <td><code>"arraybuffer"</code></td>
      <td>{{domxref("ArrayBuffer")}}</td>
    </tr>
    <tr>
      <td><code>"blob"</code></td>
      <td>{{domxref("Blob")}}</td>
    </tr>
    <tr>
      <td><code>"document"</code></td>
      <td>{{domxref("Document")}}</td>
    </tr>
    <tr>
      <td><code>"json"</code></td>
      <td><p>Objet JavaScript depuis une réponse JSON.</p></td>
    </tr>
    <tr>
      <td><code>"text"</code></td>
      <td>{{domxref("DOMString")}}</td>
    </tr>
    <tr>
      <td><code>"moz-blob"</code> {{non-standard_inline}}</td>
      <td>
        <p>
          Used by Firefox to allow retrieving partial {{domxref("Blob")}}
          data from progress events. This lets your progress event handler start
          processing data while it's still being received.
          {{gecko_minversion_inline("12.0")}}
        </p>
      </td>
    </tr>
    <tr>
      <td><code>"moz-chunked-text"</code>{{non-standard_inline}}</td>
      <td>
        <p>
          Similar to <code>"text"</code>, but is streaming. This means that the
          value in <code>response</code> is only available during dispatch of
          the <code>"progress"</code> event and only contains the data received
          since the last <code>"progress"</code> event.
        </p>
        <p>
          When <code>response</code> is accessed during a
          <code>"progress"</code> event it contains a string with the data.
          Otherwise it returns <code>null</code>.
        </p>
        <p>
          This mode currently only works in Firefox.
          {{gecko_minversion_inline("9.0")}}
        </p>
      </td>
    </tr>
    <tr>
      <td>
        <code>"moz-chunked-arraybuffer"</code>{{non-standard_inline}}
      </td>
      <td>
        <p>
          Similar to <code>"arraybuffer"</code>, but is streaming. This means
          that the value in <code>response</code> is only available during
          dispatch of the <code>"progress"</code> event and only contains the
          data received since the last <code>"progress"</code> event.
        </p>
        <p>
          When <code>response</code> is accessed during a
          <code>"progress"</code> event it contains a string with the data.
          Otherwise it returns <code>null</code>.
        </p>
        <p>
          This mode currently only works in Firefox.
          {{gecko_minversion_inline("9.0")}}
        </p>
      </td>
    </tr>
    <tr>
      <td>"ms-stream"{{non-standard_inline}}</td>
      <td>
        <p>
          Indique que la réponse est une partie d'un téléchargement d'un flux
          (?). Supporté uniquement pour les requêtes des téléchargements et
          disponible uniquement dans Internet Explorer.
        </p>
      </td>
    </tr>
  </tbody>
</table>

> **Note :** À partir de Gecko 11.0 {{geckoRelease("11.0")}} et de WebKit build 528, ces navigateurs ne permettent plus l'utilisation de l'attribut `responseType` lors des requêtes synchrones. Cela renvoi l'erreur `NS_ERROR_DOM_INVALID_ACCESS_ERR`. Ce changement a été proposé au W3C afin d'être standardisé.

## Example

```js
var url = 'somePage.html'; // une page locale

function load(url, callback) {
  var xhr = new XMLHttpRequest();

  xhr.onreadystatechange = function() {
    if (xhr.readyState === 4) {
      console.log(xhr.response); // Par défault une DOMString
    }
  }

  xhr.open('GET', url, true);
  xhr.send('');
}
```

## Specifications

| Specification                                                                | Status                               | Comment                |
| ---------------------------------------------------------------------------- | ------------------------------------ | ---------------------- |
| {{SpecName('XMLHttpRequest', '#the-response-attribute')}} | {{Spec2('XMLHttpRequest')}} | WHATWG living standard |

## Compatibilité des navigateurs

{{Compat("api.XMLHttpRequest")}}

## Voir aussi

- {{domxref("XMLSerializer")}} : Sérialisation d'un arbre DOM en XML
- Tutoriels MDN couvrant le `XMLHttpRequest` :

  - [Ajax — Pour commencer](/fr/docs/Web/Guide/AJAX/Getting_Started)
  - [Utilisation de XMLHttpRequest](/fr/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest)
  - [HTML dans XMLHttpRequest](/fr/docs/Web/API/XMLHttpRequest/HTML_in_XMLHttpRequest)
  - [Fetch API](/fr/docs/Web/API/Fetch_API)

- [HTML5 Rocks — New Tricks in XMLHttpRequest2](http://www.html5rocks.com/en/tutorials/file/xhr2/)
- Directive Feature-Policy {{httpheader("Feature-Policy/sync-xhr", "sync-xhr")}}