Fetch parse xml

hello ,

how can I parse xml with fetch in javascript?
I can’t make it work.

let myheaders = new Headers();
        myheaders.append('Accept', 'application/xml');
        myheaders.append('Content-Type', 'application/xml');
        myheaders.append('Access-Control-Allow-Origin', "https://127.0.0.1:xxxxx/Project2");
        myheaders.append('Access-Control-Allow-Credentials', 'true');
        myheaders.append("Access-Control-Allow-Headers", "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token");

  
const url="https://xxxxxxxxxxxxxx/i/1.xml"

const API_URL = "https://127.0.0.1:xxxx/Project2"

  let options = {
    method: 'GET',
    headers: myheaders,
    mode: 'no-cors',
    cache: 'default'
  };

  fetch(API_URL + url, options)
    //.then(response => {
    .then(response => response.text()
    
  
    .then(data => {
    alert('File Data = ',data)
    const parser = new DOMParser();
    const xml = parser.parseFromString(data, "text/xml");
       
         
       
       datajson=$.xml2json(xml);
      NSB.Print(datajson);   
     
    }))
    
    .catch(alert.error);
 
    }

Tip : If you’re pasting code, html or config files, surround the code with triple back ticks (```), before the first line and after the last one. It will be formatted properly. (We fixed it for you this time)

Are you getting a response in XML format?

yes, I expect a response from an xml file.

Yes, but are you actually getting one?

Yes, I receive an answer but the content is empty.The problem is that it won’t let me install: “npm install node-fetch”.I think that the registration of the packages is expired, no certificate.

How are you running your app? As a web app? As a node app? As a native app?

If you’re running as a web app, you do not need to do npm install node-fetch, since node is part of normal JavaScript.

thanks, already fix the problem . The problem was the cors

Can you tell a bit more about what you did to solve the problem, so the next person who has it knows what to do?

I was able to fix it by putting in the headers:

<?php
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Credentials', 'true');
header('Access-Control-Allow-Headers');
echo file_get_contents($_GET['urlToGet']);
?>

Tip : If you’re pasting code, html or config files, surround the code with triple back ticks (```), before the first line and after the last one. It will be formatted properly. (We fixed it for you this time)