-
QML + XmlHttpRequest
Hi guys,
I'm completely fresh to Nokia/QT development.
I have stumbled upon the following problem:
Let's say I have Qt Quick app with only one button and a text field for logs.
Pressing this button launches JavaScript function with the following code:
[CODE]function getToken() {
var response = "";
var http = new XMLHttpRequest();
http.onreadystatechange = function()
{
response += http.readyState.toString() + "\n";
if (http.readyState == 4)
{
response = "Data has been sent\n";
if (http.status == 200)
{
response += http.responseText + "\n";
}
else
{
response += "Response error\n" + http.status;
}
}
}
http.open("GET", "http://www.bez-kabli.pl"); //or whatever www site
http.send(null);
text_edit1.text = response;
}[/CODE]
The problem is that the only thing I get in log is state "1", so connection is made and XmlHttpRequest object is stuck on this state.
I run this app on Nokia Simulator. The site in url is working ok, computer is connected via router w/NAT (no proxy). Router firewall monitoring is clearly stating that connection is made.
I tried with POST, but same result.
Any ideas where I went wrong? How could I debug this?
best regards
[B][U]EDIT:[/U][/B]
I got it to work... sort of :)
Now it looks like this (I split it into modules to make debug easier and added some exception handling):
[code]
var http = null;
var response = "";
function getResponse()
{
try
{
if (http.readyState == 4)
{
response = "Your data has been sent\n";
if (http.status == 200)
{
response += http.responseText + "\n";
}
else
{
response += "Response error\n" + http.status;
}
}
}
catch (e)
{
response += "getResponse() error:" + e;
}
}
function getToken() {
try
{
http = new XMLHttpRequest();
http.onreadystatechange = getResponse;
http.open("GET", "http://www.bez-kabli.pl");
http.setRequestHeader("Connection", "close");
http.send();
}
catch (e)
{
response += "getToken() error:" + e;
}
text_edit1.text = response;
}
[/code]
The "sort of" part is that I click the button once - nothing, I click it again - and there it is (whole HTML page code). A little confusing for me.