Page 1 of 1

Access an XML file from different domain in JS

Posted: 29 Nov 2020, 06:46
by Marius
I am creating a website with HTML and JavaScript that relies on the data of an XML file hosted on a separate domain.
I can achieve this with jQuery-Ajax if the XML is in the same domain as my HTML page, but I cannot find a solution when the file is on a different domain.
Any ideea of how can i read a file from different domain using JavaScript in html page (Not server side)?

Access an XML file from different domain in JS

Posted: 29 Nov 2020, 07:24
by MarPlo
Try using the the fetch api.

Code: Select all

fetch('//example.com/file_address')
.then( response => response.text() )
.then( response => {
  //response is a string containing xml...
  document.getElementById('elm_id').innerHTML = response;
})
.catch( console.error );
I thing you don't need to serialize the xml, you should just be able to put it directly... but not sure.