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);
}
ghenne
August 14, 2021, 1:50pm
2
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)
ghenne
August 14, 2021, 1:53pm
3
Are you getting a response in XML format?
yes, I expect a response from an xml file.
ghenne
August 15, 2021, 3:44pm
5
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.
ghenne
August 16, 2021, 10:21am
7
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
ghenne
August 18, 2021, 9:48am
9
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']);
?>
ghenne
August 21, 2021, 10:27am
11
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)