Depending on the Ajax.js class library, the following code is a simple implementation of the task of getting the weather JSON data and showing it
"TESTAJAX.QML"
1Import QtQuick 2.02Import Qtquick.controls 1.23Import "Ajax.js"As Ajax4 5 6 /**7 test using AJAX to get JSON data8 */9 grid{Tenwidth:600 Oneheight:400 ASpacing:10 -Columns:2 - theText {text: ' City '} - Text {id:city} - -Text {text: ' Date '} + Text {id:date} - +Text {text: ' temp '} A Text {ID:TEMP1} at - component.oncompleted: { -Ajax.get ("http://m.weather.com.cn/atad/101230201.html", - function(result, JSON) { -City.text =json.weatherinfo.city; -Date.text =json.weatherinfo.date_y; inTemp1.text =Json.weatherinfo.temp1; - } to ); + } -}
"Ajax.js"
1 //GET2 functionget (URL, success, failure)3 {4 varXHR =NewXMLHttpRequest;5Xhr.open ("GET", URL);6Xhr.onreadystatechange =function() {7 Handleresponse (XHR, success, failure);8 }9 xhr.send ();Ten } One A //POST - functionpost (URL, arg, success, failure) - { the varXHR =NewXMLHttpRequest; -Xhr.open ("POST", URL); -Xhr.setrequestheader ("Content-length", arg.length); -Xhr.setrequestheader ("Content-type", "application/x-www-form-urlencoded;");//You must have this sentence when you use post. +Xhr.onreadystatechange =function() { - Handleresponse (XHR, success, failure); + } A Xhr.send (ARG); at } - - - - //Handling Return Values - functionHandleresponse (XHR, success, failure) { in if(Xhr.readystate = =xmlhttprequest.done) { - if(Xhr.status = = 200){ to if(Success! =NULL&& Success! =undefined) + { - varresult =Xhr.responsetext; the Try{ * success (result, json.parse (result)); $}Catch(e) {Panax Notoginseng success (result, {}); - } the } + } A Else{ the if(Failure! =NULL&& Failure! =undefined) + failure (Xhr.responsetext, xhr.status); - } $ } $}
QML Ajax Get JSON Data sample