Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
xmlhttprequest.qdoc
Go to the documentation of this file.
1// Copyright (C) 2023 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
3
4/*!
5 \page qml-qtqml-xmlhttprequest.html
6 \title The XMLHttpRequest JavaScript Object
7 \inqmlmodule QtQml
8 \brief Object for sending requests to a server.
9
10 The \c XMLHttpRequest object allows scripts to perform HTTP client functionality,
11 such as submitting form data or loading data asynchronously from a server.
12
13 The \c XMLHttpRequest API is a partial implementation of the
14 \l {https://www.w3.org/TR/2014/WD-XMLHttpRequest-20140130/}{W3C XHR Level 1 standard}
15 with the following exception:
16 \list
17 \li It does not enforce the same origin policy.
18 \endlist
19
20 \section1 Sending requests
21
22 Use the following steps to send a request using the \c XMLHttpRequest API:
23 \list 1
24 \li Create a \c XMLHttpRequest object.
25 \li Assign a callback function to the \l {function XMLHttpRequest::onreadystatechange}{onreadystatechange} signal handler.
26 \li Call \l {void XMLHttpRequest::open(method, url, async)}{open()} with the appropriate HTTP method and URL to request.
27 \li Call \l {void XMLHttpRequest::send(data)}{send()}
28 \endlist
29
30 The callback function handles the HTTP response for a request.
31 It's a good practice to check if the \l{enumeration XMLHttpRequest::readyState}{readyState} is \c DONE in the handler,
32 before you use one of the following methods to read the response:
33 \list
34 \li \l {var XMLHttpRequest::response}{response}
35 \li \l {string XMLHttpRequest::responseText}{responseText}
36 \li \l {var XMLHttpRequest::responseXML}{responseXML}
37 \endlist
38
39 The following example demonstrates how to send a request and read the response:
40
41 \snippet qml/XHRForm.qml 0
42
43 The earlier snippet connects the button's clicked signal to an external \c sendRequest function.
44 A resource URL is passed as the first argument, and a callback function to handle UI updates is passed as the second.
45 The \c sendRequest function, which exists in an external \c request.js file, can be implemented like this:
46
47 \snippet qml/xmlhttprequest.js 0
48
49 The earlier snippet follows the four simple steps mentioned at the beginning.
50 It instantiates the \c XMLHttpRequest object first, and assigns a callback function to handle the response.
51 It also calls \l {void XMLHttpRequest::open(method, url, async)}{open()} with an HTTP method and URL, before it sends the request to the server.
52 Notice that the second argument to \c sendRequest is called at the end of \l {function XMLHttpRequest::onreadystatechange}{onreadystatechange},
53 in order to handle UI updates based on the HTTP response.
54
55 Set the \c QML_XHR_DUMP environment variable to \c 1 if you want to debug a request.
56 This will log the following information:
57 \list
58 \li Method type (GET or POST), URL, and body of sent requests.
59 \li URL and body of responses received.
60 \li Network error, if any.
61 \endlist
62
63 \section1 Accessing local files
64
65 By default, you cannot use the \c XMLHttpRequest object to read files from your local file system.
66 If you wish to use this feature to access local files, you can set the following environment variables to \c 1.
67 \list
68 \li QML_XHR_ALLOW_FILE_READ
69 \li QML_XHR_ALLOW_FILE_WRITE
70 \endlist
71 \warning Use this feature only if you know that the application runs trusted QML and JavaScript code.
72
73 \section1 responseXML document
74
75 The \c responseXML XML DOM tree currently supported by QML is a reduced subset of
76 the \l {http://www.w3.org/TR/DOM-Level-3-Core/}{DOM Level 3 Core} API supported in a web browser.
77 The following objects and properties are supported by the QML implementation:
78
79 \table
80 \header
81 \li \b {Node}
82 \li \b {Document}
83 \li \b {Element}
84 \li \b {Attr}
85 \li \b {CharacterData}
86 \li \b {Text}
87
88 \row
89 \li
90 \list
91 \li nodeName
92 \li nodeValue
93 \li nodeType
94 \li parentNode
95 \li childNodes
96 \li firstChild
97 \li lastChild
98 \li previousSibling
99 \li nextSibling
100 \li attributes
101 \endlist
102
103 \li
104 \list
105 \li xmlVersion
106 \li xmlEncoding
107 \li xmlStandalone
108 \li documentElement
109 \endlist
110
111 \li
112 \list
113 \li tagName
114 \endlist
115
116 \li
117 \list
118 \li name
119 \li value
120 \li ownerElement
121 \endlist
122
123 \li
124 \list
125 \li data
126 \li length
127 \endlist
128
129 \li
130 \list
131 \li isElementContentWhitespace
132 \li wholeText
133 \endlist
134
135 \endtable
136
137 \section1 void XMLHttpRequest::abort()
138
139 Cancels the current request.
140
141 It changes the \l {enumeration XMLHttpRequest::readyState}{readyState} property to be \c XMLHttpRequest.UNSENT and emits the \c readystatechange signal.
142
143 \section1 string XMLHttpRequest::getAllResponseHeaders()
144
145 Returns a \c String of headers received from the last response.
146
147 The following is an example response header:
148 \code
149 content-encoding: gzip
150 content-type: text/html; charset=UTF-8
151 date: Mon, 06 Feb 2023 09:00:08 GMT
152 expires: Mon, 13 Feb 2023 09:00:08 GMT
153 last-modified: Thu, 17 Oct 2019 07:18:26 GMT
154 \endcode
155
156 \sa {string XMLHttpRequest::getResponseHeader(headerName)}{getResponseHeader()}
157
158 \section1 string XMLHttpRequest::getResponseHeader(headerName)
159
160 Returns either the \a headerName value from the last response, or an empty \c String, if the \a headerName is missing.
161
162 \sa {string XMLHttpRequest::getAllResponseHeaders()}{getAllResponseHeaders()}
163
164 \section1 void XMLHttpRequest::open(method, url, async)
165
166 Specify the HTTP \a method you want the request to use, as well as the \a url you wish to request.
167 You should make sure to always call this function before \l {void XMLHttpRequest::send(data)}{send()}.
168 An optional third parameter \a async can be used to decide whether the request should be asynchronous or not.
169 The default value is \c true.
170
171 Emits the \c readystatechange signal, which calls the \l {function XMLHttpRequest::onreadystatechange}{onreadystatechange} handler with
172 the \l {enumeration XMLHttpRequest::readyState}{readyState} property set to \c XMLHttpRequest.OPENED.
173
174 \section1 void XMLHttpRequest::send(data)
175
176 Sends the request to the server.
177 You can use the optional argument \a data to add extra data to the body of the request.
178 This can be useful for POST requests, where you typically want the request to contain extra data.
179
180 The \l {enumeration XMLHttpRequest::readyState}{readyState} property is updated once a response has been received from the server,
181 and while the response is being processed. It will first be set to \c HEADERS_RECEIVED, then to \c LOADING,
182 and finally \c DONE, once the response has been fully processed.
183 The \c readystatechange signal is emitted every time \l {enumeration XMLHttpRequest::readyState}{readyState} is updated.
184
185 \section1 void XMLHttpRequest::setRequestHeader(header, value)
186
187 Adds a new header to the next request you wish to send.
188 This is a key-value pair, which has the name \a header and the corresponding \a value.
189
190 \section1 void XMLHttpRequest::overrideMimeType(mime)
191 \since 6.6
192
193 Forces the \c XMLHttpRequest to interpret the data received in the next HTTP response,
194 as if it had the MIME type \a mime, rather than the one provided by the server.
195
196 \section1 function XMLHttpRequest::onreadystatechange
197
198 Choose a callback function you want to get invoked whenever the \l {enumeration XMLHttpRequest::readyState}{readyState} of the \c XMLHttpRequest object changes.
199
200 \sa {enumeration XMLHttpRequest::readyState}{readyState}
201
202 \section1 enumeration XMLHttpRequest::readyState
203 \readonly
204
205 Indicates the current state of the \c XMLHttpRequest object.
206
207 The value can be one of the following:
208 \value XMLHttpRequest.UNSENT The request is not initialized, which is the state before calling \l {void XMLHttpRequest::open(method, url, async)}{open()}.
209 \value XMLHttpRequest.OPENED The request is initialized, meaning \l {void XMLHttpRequest::open(method, url, async)}{open()} was previously called, but no further progress.
210 \value XMLHttpRequest.HEADERS_RECEIVED Received a reply from the sever, but the request is not fully processed yet.
211 \value XMLHttpRequest.LOADING Downloading data from the server.
212 \value XMLHttpRequest.DONE Finished processing the request.
213
214 \sa {function XMLHttpRequest::onreadystatechange}{onreadystatechange}
215
216 \section1 string XMLHttpRequest::responseURL
217 \readonly
218 \since 6.6
219
220 Returns the url that was used to retrieve the response data, after any redirects have occurred.
221
222 \section1 string XMLHttpRequest::responseText
223 \readonly
224
225 Returns a \c String containing the data received from the last response.
226
227 \sa {var XMLHttpRequest::responseXML}{responseXML}
228
229 \section1 var XMLHttpRequest::responseXML
230 \readonly
231
232 Returns either a \c Document, or \c null, if the response content cannot be parsed as XML or HTML.
233 See the \l {responseXML document}{responseXML document} section for more information.
234
235 \sa {string XMLHttpRequest::responseText}{responseText}
236
237 \section1 var XMLHttpRequest::response
238 \readonly
239
240 Returns either a \c String, an \c ArrayBuffer, or a \c Document,
241 depending on the \l {string XMLHttpRequest::responseType}{responseType} of the last request.
242
243 \sa {string XMLHttpRequest::responseType}{responseType}, {string XMLHttpRequest::responseText}{responseText}, {var XMLHttpRequest::responseXML}{responseXML}
244
245 \section1 string XMLHttpRequest::responseType
246
247 Returns a \c String describing the content type of the last response received.
248 \list
249 \li If the response type is "text" or an empty \c String, the response content is a UTF-16 encoded \c String.
250 \li If the response type is "arraybuffer", it means that the response content is an \c ArrayBuffer containing binary data.
251 \li If the response type is "json", the response content should be a JSON \c Document.
252 \li If the response type is "document", it means that the response content is an XML \c Document, which can be safely read with the \l {var XMLHttpRequest::responseXML}{responseXML} property.
253 \endlist
254
255 \sa {var XMLHttpRequest::response}{response}
256
257 \section1 int XMLHttpRequest::status
258 \readonly
259
260 Returns the status code for the last response received.
261
262 \sa {string XMLHttpRequest::statusText}{statusText}
263
264 \section1 string XMLHttpRequest::statusText
265 \readonly
266
267 Returns a \c String that has the status message associated with the status code for the last response received.
268
269 \sa {int XMLHttpRequest::status}{status}
270*/