Ask / Submit
0

XMLHttpRequest() .responseText cutting off data [answered]

asked 2014-01-19 11:23:23 +0300

Mariusmssj gravatar image

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?

edit retag flag offensive reopen delete

The question has been closed for the following reason "the question is answered, an answer was accepted" by Mariusmssj
close date 2014-02-02 10:13:56.949316

Comments

as .responseText using console.log(httpReq.responseText) only gives me a partial xml data around 20% of what's inside the xml.

Couldn't that be a limitation of console.log()? Have you tried actually using the returned data?

Tanghus ( 2014-01-19 13:48:11 +0300 )edit

Have 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 +0300 )edit

It 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 +0300 )edit

1 Answer

Sort by » oldest newest most voted
0

answered 2014-01-19 14:01:43 +0300

Mariusmssj gravatar image

updated 2014-01-19 14:13:18 +0300

@Tanghus Yes I have tried using data received and it worked but it only updates the data on every second request, which I need it to be up to date right away. I changed the function a bit I am getting odd output:

    httpReq.onreadystatechange = function()
    {
        if(httpReq.readyState === 4 && httpReq.status == 200)
        {
            xmlModel.xml = httpReq.responseText
            console.log(xmlModel.get(0).fullscreen)
        }
        else
            console.log("Not all data came in")
    }
    httpReq.send(null);
}

output:

Not all data came in
Not all data came in
<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<root>
<fullscreen>false</fullscreen>
<aspectratio>default</aspectratio>
edit flag offensive delete publish link more

Comments

You should convert this to a comment, as it's not an answer to your question. Click "More..."->"Repost as question comment"

Tanghus ( 2014-01-19 14:08:13 +0300 )edit

Can't it has more than 300 characters

Mariusmssj ( 2014-01-19 14:13:46 +0300 )edit

Question tools

Follow
2 followers

Stats

Asked: 2014-01-19 11:23:23 +0300

Seen: 1,546 times

Last updated: Jan 19 '14