XMLHttpRequest()。responseSext不返回完整数据

时间:2014-01-18 22:44:24

标签: javascript xml xmlhttprequest qml responsetext

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

另一个问题是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();
}

我做错了吗?

1 个答案:

答案 0 :(得分:0)

某些浏览器没有像XMLHttpRequest.DONE这样的常量。使用数值:

if(httpReq.readyState === 4)
相关问题