I am making an AJAX get request which returns an XML object containing a fully-qualified SVG. In the browser’s inspector I can see the response header indicates it is ‘application/xml’, and I can see the response itself: <svg> .... </svg>
, correctly structured with namespace headers etc.
I then try to render the object within a and it is rendering [object Object] instead of the content itself. If the AJAX call populated the var ‘src’, I write:
element = document.getElementById('myDiv');
element.innerHTML = src;
I have looked at various other articles about this, e.g. using DOMParser, but the best I get is that it renders something like [object XMLElement] etc. Very frustating.
Is there a simple way to just have the actual SVG put between the tags so it renders the graphics therein?
3
Answers
So the entire problem was that in my $.ajax call, I had the parameter
dataType: 'application/xml'
. By changing the value to just'xml'
or even removing it and letting it guess, then my response was a valid XMLDocument.By having the incorrect value, it created an empty Object which was the root of all my problems.
Thanks to everyone for their assistance regardless.
All you need is a native JavaScript Web Component
<load-file src="">
Full documentation: https://dev.to/dannyengelman/load-file-web-component-add-external-content-to-the-dom-1nd
You should probably get the
rootElement.outerHTML
from the object, because it is a XML document that is returned.The example should show a blue square. I use a data URL for the request, but that can be replaced by a normal URL.