-
-
Save lambdaX/a3e4bbbed83a302b7429 to your computer and use it in GitHub Desktop.
Simple HTTP JSON Request in QML
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import bb.cascades 1.0 | |
Page { | |
content: Container { | |
Label { | |
id: emailLabel | |
text: qsTr("Email") | |
} | |
Label { | |
id: urlLabel | |
text: qsTr("URL") | |
} | |
Label { | |
id: sinceLabel | |
text: qsTr("Since") | |
} | |
Label { | |
id: bioLabel | |
text: qsTr("Bio") | |
} | |
Button { | |
id: requestButton | |
text: "World Domination" | |
onClicked: { | |
request('http://someurlgoeshere.com', function (o) { | |
// log the json response | |
console.log(o.responseText); | |
// translate response into object | |
var d = eval('new Object(' + o.responseText + ')'); | |
// access elements inside json object with dot notation | |
emailLabel.text = d.email | |
urlLabel.text = d.url | |
sinceLabel.text = d.since | |
bioLabel.text = d.bio | |
}); | |
} | |
} | |
} | |
// this function is included locally, but you can also include separately via a header definition | |
function request(url, callback) { | |
var xhr = new XMLHttpRequest(); | |
xhr.onreadystatechange = (function(myxhr) { | |
return function() { | |
callback(myxhr); | |
} | |
})(xhr); | |
xhr.open('GET', url, true); | |
xhr.send(''); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"email": "[email protected]", | |
"url": "http://www.microsoft.com", | |
"since": "2011-09-08T08:06:48Z", | |
"bio": "I am awesome" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment