0

我正在使用 XMLHttpRequest() 从网页获取 xml 数据并在 XmlListModel 中使用它。我遇到的问题似乎是 XmlListModel 只获得了一小部分数据,因为 .responseText 使用console.log(httpReq.responseText)只给了我大约 20% 的部分 xml 数据。

另一个问题是 XmlListModel 总是在一个调用之后,当我第一次调用它说全屏未定义的函数时,但是当我再次调用它时它很好。但是这个函数需要每 1 秒调用一次才能获取更新的数据,因为 xml 文件总是在变化,但只有第二次调用才能给我正确的数据。

该函数如下所示:

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();
}

难道我做错了什么?

4

1 回答 1

0

有些浏览器没有像XMLHttpRequest.DONE. 使用数值:

if(httpReq.readyState === 4)
于 2014-01-18T22:53:35.550 回答