XMLHttpRequest() .responseText cutting off data [answered]
0 down vote favorite
I am using XMLHttpRequest() to get xml data from a webpage and use it in a XmlListModel. The problem that I got seems to be that XmlListModel only gets a small portion of the data as .responseText using console.log(httpReq.responseText) only gives me a partial xml data around 20% of what's inside the xml.
The other issue is that XmlListModel is always behind one call, when I first call the function it says fullscreen is undefined but when I call it again it's fine. BUT this function needs to be called ever 1 second to get updated data as the xml file is always changing but only ever second call gives me right data.
The function looks like this:
XmlListModel{
id: xmlModel
query: "/root"
XmlRole{ name: "fullscreen"; query: "fullscreen/string()"}
XmlRole{ name: "volume"; query: "volume/string()"}
XmlRole{ name: "random"; query: "random/string()"}
XmlRole{ name: "state"; query: "state/string()"}
XmlRole{ name: "loop"; query: "loop/string()"}
XmlRole{ name: "repeat"; query: "repeat/string()"}
}
function getVLCstatus()
{
var httpReq = new XMLHttpRequest()
var url = "http://" + ip + ":" + port + "/requests/status.xml";
httpReq.open("GET", url, true);
// Send the proper header information along with the request
httpReq.setRequestHeader("Authorization", "Basic " + Qt.btoa(username + ":" + password));
httpReq.setRequestHeader('Content-Type', 'text/xml');
httpReq.onreadystatechange = function()
{
if(httpReq.readyState === XMLHttpRequest.DONE)
{
if(httpReq.status == 200)
{
xmlModel.xml = httpReq.responseText
console.log(xmlModel.get(0).fullscreen)
}
}
}
httpReq.send();
}
Am I doing something wrong?
Couldn't that be a limitation of console.log()? Have you tried actually using the returned data?
Tanghus ( 2014-01-19 13:48:11 +0200 )editHave you tried making the same request every second from another platform e.g. a desktop browser?
Besides that doing a request every second is not very mobile friendly neither dataplan-wise nor battery-wise.
Tanghus ( 2014-01-19 14:14:18 +0200 )editIt would only do it for 5 seconds, and then only every 30 seconds. But because user can change the xml and it's read with update delay application gets confused, as it just updated the UI but xml has old data and it all goes wrong from there.
Mariusmssj ( 2014-01-19 14:19:53 +0200 )edit