Load xml from javascript

后端 未结 3 1121
不思量自难忘°
不思量自难忘° 2021-01-03 14:55

Totally new to XML and I\'ve been struggling on this very simple objective for too long (though I can find enough on the internet about it). Just need the values out of this

3条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-03 15:32

    Your server isn't returning the appropriate Content-Type header. The responseXML property only works if the server returns a Content-Type: text/xml or similar +xml header.

    See Ajax Patterns:

    The service just needs to output an XML Content-type header...

    From the w3c:

    If final MIME type is not null, text/xml, application/xml, and does not end in +xml [...] return null.

    If you have no access to the server and can't change the Content-Type header, use the overrideMimeType function to force the XMLHttpRequest to treat the response as text/xml:

    if (window.XMLHttpRequest) {
       xhttp = new XMLHttpRequest();
    } else {    // IE 5/6
       xhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    
    xhttp.overrideMimeType('text/xml');
    
    xhttp.open("GET", "pricing.xml", false);
    xhttp.send(null);
    xmlDoc = xhttp.responseXML;
    
    var uurloon = xmlDoc.getElementsByTagName("uurloon")[0].childNodes[0].text;
    var setloon = xmlDoc.getElementsByTagName("setloon")[0].childNodes[0].text
    alert('end');
    

    citation: http://blog-rat.blogspot.com/2010/11/xmlhttprequestresponsexml-returns-null.html

提交回复
热议问题