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