简体   繁体   English

XMLHttpRequest().responseText不返回完整数据

[英]XMLHttpRequest() .responseText not returning full data

I am using XMLHttpRequest() to get xml data from a webpage and use it in a XmlListModel. 我正在使用XMLHttpRequest()从网页获取xml数据,并在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. 我遇到的问题似乎是XmlListModel仅使用console.log(httpReq.responseText)以.responseText的形式获取一小部分数据,仅给了我大约xml内容的20%的部分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. 另一个问题是XmlListModel总是在一个调用之后,当我第一次调用该函数时,它说未定义全屏显示,但是当我再次调用它时就没事了。 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. 但是此功能需要每秒调用一次以获取更新的数据,因为xml文件始终在变化,但是只有每秒调用才能给我正确的数据。

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? 难道我做错了什么?

Some browsers doesn't have the constants like XMLHttpRequest.DONE . 一些浏览器没有像XMLHttpRequest.DONE这样的常量。 Use the numeric value: 使用数值:

if(httpReq.readyState === 4)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM