XMLHttpRequest problem
-
Hello, I'm a newcomer in Qt Quick.
I want to use XMLHttpRequest class in order to use Twitter API. I wrote the following code:
@
import QtQuick 1.0Rectangle {
id: container
width: 360
height: 360
Text {
text: "Hello World"
anchors.centerIn: parent
}
MouseArea {
anchors.fill: parent
onClicked: {
container.testFunction();
}
}
function testFunction(){
var xhr = new XMLHttpRequest();
xhr.onreadystatchange = function(){
if (xmlhttp.readyState == 4) {
if(xmlhttp.status == 200) {
alert(xmlhttp.responseText);
}
}
};
xhr.open('GET','http://www.google.com',true);
xhr.send();
}
}
@
But, unfortunately xmlhttp.readyState always returns "1". Would you please correct me, or drop a hint on this problem?
Thank you! -
Hi,
With a few small tweaks:
- xmlhttp -> xhr
- alert -> console.log
- onreadystatchange -> onreadystatechange
The following version works for me:
@
import QtQuick 1.0Rectangle {
id: container
width: 360
height: 360
Text {
text: "Hello World"
anchors.centerIn: parent
}
MouseArea {
anchors.fill: parent
onClicked: {
container.testFunction();
}
}
function testFunction(){
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function(){
console.log("readyState updated: " + xhr.readyState)
if (xhr.readyState == 4) {
if(xhr.status == 200) {
console.log(xhr.responseText);
}
}
};
xhr.open('GET','http://www.google.com');
xhr.send();
}
}
@Does this modified version work for you, or are you still only getting a state of 1? What platform are you running on? (with e.g. Symbian it could be that your app doesn't have sufficient networking privileges?)
Regards,
Michael -
Thank you, Michael!!!
I tried to run it from my office, which has a proxy configured, that's why it fails to run correctly, but then I reran it from my home laptop, with the fixes you mentioned, and it works just fine now! Thx, sir!!! -
Great! Note that if you do need to get it working in a proxy situation, you should be able to use "QDeclarativeEngine::setNetworkAccessManagerFactory":http://doc.qt.nokia.com/4.7-snapshot/qdeclarativeengine.html#setNetworkAccessManagerFactory and the associated functionality.
Regards,
Michael