简体   繁体   中英

WinJS.xhr returning XML string as text (including \n\r tags), instead of a responseXML

I'm starting up playing with Win8 development and I'm stucked in a problem since yesterday.

I've followed the MSDN example HERE to grab the data, I can retrieve the data (therefore, isn't a connection limitation issue) but the problem is that regardless the settings I use, it always retrieve data as plain text, including \\r\\n characters.

I assume that if I could retrieve the structured XML would make my job easier, so I'm hoping you folks can put some lights on what I'm doing wrong.

Here's my code snippet:

<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>

Here's the value of 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"

And HERE is a similar question, which seems to be working using the responseXML responseType (although it's not documented @MSDN guide).

Some things I already tried:

  • Use a responseType as 'document' (as per the MSDN guide) and then retrieve result.responseXML;
  • Omit the responseType argument;
  • Use the approach above.

Now, I ran out of ideas. Any thoughts?

Try To use the following code to get the tags you want to play... (I believe it will do exactly what you want/need, connecting to a webpage and than work on the result based o the webpage/xml tags

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

I think you should set an option for responseType : "document" just like:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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