forked from patrickelectric/agora-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJSONListModel.qml
48 lines (43 loc) · 1.52 KB
/
JSONListModel.qml
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
import QtQuick 2.11
Item {
id: rootItem
property string source
property string requestMethod: "GET"
property string requestParams
property string errorString: ""
property int httpStatus: 0
property var json
state: "null"
states: [
State { name: "null" },
State { name: "ready" },
State { name: "loading" },
State { name: "error" }
]
function load() {
var xhr = new XMLHttpRequest;
xhr.open(requestMethod, (requestMethod === "GET") ? source + "?" + requestParams : source);
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xhr.onerror = function() {
rootItem.errorString = qsTr("Cannot connect to server!");
rootItem.state = "error";
}
xhr.onreadystatechange = function() {
if (xhr.readyState === XMLHttpRequest.DONE) {
rootItem.httpStatus = xhr.status;
if (rootItem.httpStatus >= 200 && rootItem.httpStatus <= 299) {
json = JSON.parse(xhr.responseText);
rootItem.state = "ready";
}
else {
rootItem.errorString = qsTr("The server returned error ") + xhr.status;
rootItem.state = "error";
}
}
}
rootItem.errorString = ""
rootItem.state = "loading";
json = undefined
xhr.send(requestParams); // requestParams ignored if requestMethod equals GET
}
}