Ajax request and text/xml
-
09-09-2019 - |
Question
I am requesting and xml file over ajax, the server uses the header text/xml for the data returned. Firefox reads this header and turns the data into an XMLDocument object which means I can't use it with jQuery. How can I get my XML document as plain text?
Solution
The xmlHttp
object returned has a responseXML property. This maps to an XmlDocument. If you read the textContent
of the childnodes of this document, you will be able to retrieve the plain text response.
For instance:
// Works on FF. For IE, you can read the lastChild.text property.
var responseText = xmlHttp.responseXML.lastChild.textContent;
Alternatively, you can access the responseText property to get the entire response as a string:
// Works on both IE and FF.
var responseText = xmlHttp.responseText;
OTHER TIPS
Fixed it by serializing:
var serializer = new XMLSerializer();
var text = serializer.serializeToString(xmldoc);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow