-
Notifications
You must be signed in to change notification settings - Fork 366
/
Copy pathDemoJson.qml
51 lines (46 loc) · 1.55 KB
/
DemoJson.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
49
50
51
import QtQuick 2.12
ListView {
id: view
spacing: 10
//model:['red','green','blue']
delegate: Rectangle{
width: ListView.view.width
height: 30
color: modelData
}
//请求网络数据
function request() {
let xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if(xhr.readyState === 4) {
print('request DONE',xhr.status);
if(xhr.status === 200){
print(xhr.responseText.toString())
view.model = JSON.parse(xhr.responseText.toString());
}
}
}
xhr.open("POST", "http://127.0.0.1:54321/json");
xhr.setRequestHeader("Content-type", "application/json");
//xhr.send("['red','green','blue','orange','cyan']");
xhr.send(JSON.stringify(['red','green','blue','orange','cyan']));
}
//访问本地文件
function requestLocal() {
let xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if(xhr.readyState === 4) {
print('request DONE',xhr.status);
if(xhr.status === 200){
print(xhr.responseText.toString())
view.model = JSON.parse(xhr.responseText.toString());
}
}
}
//json文件的文本内容,字符串双引号:["red","green","blue"]
xhr.open("GET", "qrc:/localfile.json");
xhr.send();
}
Component.onCompleted: request()
//Component.onCompleted: requestLocal()
}