WinJS.xhr将XML字符串作为文本(包括\ n \ r \ tag)返回,而不是responseXML

时间:2012-11-11 15:52:33

标签: javascript windows-8 winjs

我开始玩Win8开发,我从昨天开始就遇到了问题。

我已经按照MSDN示例HERE来获取数据,我可以检索数据(因此,不是连接限制问题),但问题是无论我使用什么设置,它总是检索数据为纯文本,包括\ r \ n字符。

我认为如果我能够检索结构化的XML会让我的工作变得更轻松,那么我希望大家可以对我做错的事情进行一些说明。

这是我的代码段:

<div id="xhrReport"></div>
<script>
    var xhrDiv = document.getElementById("xhrReport");
    xhrDiv.style.color = "#000000";

    WinJS.xhr({ url: "http://www.w3schools.com/xml/note.xml", responseType: "responseXML"})
        .done(
            function complete(result) {
                var xmlResponse = result.response; 

                xhrDiv.innerText = "Downloaded the page";
                xhrDiv.style.backgroundColor = "#00FF00"; //here goes my breakpoint to check response value

            },
            function error(result) {
                xhrDiv.innerHTML = "Got error: " + result.statusText;
                xhrDiv.style.backgroundColor = "#FF0000";
            },
            function progress(result) {
                xhrDiv.innerText = "Ready state is " + result.readyState;
                xhrDiv.style.backgroundColor = "#0000FF";
            }
        );

</script>

这是xmlResponse的价值

"<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\r\n<!-- Edited by XMLSpy® -->\r\n<note>\r\n\t<to>Tove</to>\r\n\t<from>Jani</from>\r\n\t<heading>Reminder</heading>\r\n\t<body>Don't forget me this weekend!</body>\r\n</note>\r\n"

HERE是一个类似的问题,似乎使用responseXML responseType工作(尽管没有记录@MSDN指南)。

我已经尝试过的一些事情:

  • 将responseType用作'document'(根据MSDN指南),然后检索result.responseXML;
  • 省略responseType参数;
  • 使用上述方法。

现在,我没有想法了。有什么想法吗?

2 个答案:

答案 0 :(得分:2)

尝试使用以下代码来获取您想要播放的标签...(我相信它会完全符合您的需要,连接到网页,而不是根据网页/ xml标签处理结果

function connectToURL(){
    var url = "";

    xmlHttp = GetXmlHttpObject();
    if (xmlHttp == null) {
         return;
    }            
    xmlHttp.onreadystatechange = stateChanged;
    xmlHttp.open("GET", url,true);
    xmlHttp.send(null);
}

// your job will actually start on this one...
function stateChanged() {
        if(xmlHttp != null )
            if (xmlHttp[item.key].readyState == 4 ) {
                try {
                    var xmlDoc = xmlHttp.responseXML.documentElement.getElementsByTagName("TAGYOUWANTTOGET");
                    for (var i = 0; i < xmlDoc.length; i++) {
                       xmlDoc[i].getElementsByTagName("TAG")[0].childNodes[0].nodeValue
                    }
                } catch (e) {
                   //work on the exception
                }
            }
        }     
}

function GetXmlHttpObject(){
    var xmlHttp=null;
    try{
        xmlHttp = new XMLHttpRequest();
    }
    catch(e){
        try{
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch(e){
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    return xmlHttp;
}

答案 1 :(得分:0)

我认为你应该为responseType设置一个选项:“document”就像:

  WinJS.xhr({
        url: "http://www.capital.bg/rss/?rubrid=" + groupId,
        responseType:"document"

    }).then(function (result) {
        console.dir(result.response);
    });