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
|
import QtQuick 2.7
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.3
ApplicationWindow {
id: applicationWindow
visible: true
width: 640
height: 480
title: qsTr("Salut Cyril !")
// Fonction requette AJAX
function request(url, callback) {
var http = new XMLHttpRequest();
http.open("GET", url, true);
http.onreadystatechange = function () {
if(http.readyState === XMLHttpRequest.DONE && http.status === 200) {
callback(http)
}
};
http.send();
}
GridLayout {
id: grid
columnSpacing: 10
rowSpacing: 10
anchors.verticalCenter: parent.verticalCenter
anchors.horizontalCenter: parent.horizontalCenter
rows: 2
columns: 1
Button {
id: requestButton
text: "Ajax"
onClicked: {
request('http://localhost/legumes.php?action=get_list_recettes&Num_Legume=32',
function (o) {
// affiche dans la console le retour JSON
console.log(o.responseText);
// Convertir la reponse en objet
var d = eval('new Object(' + o.responseText + ')');
// Affiche les elements depuis le json
var JsonObject = JSON.parse(o.responseText);
console.log(d);
var aString = JsonObject.Num_Legume;
var bString = JsonObject.Image_legume;
txtResponse.text = "Titre: " + bString + " Corps: " + aString ;
}
);
}
}
Label {
id: txtResponse
text: "Réponse..."
anchors.horizontalCenter: parent.horizontalCenter
}
}
} |
Partager