我正在尝试将变量json传递给global。我已经看过如何使用函数执行此操作,但是在这种情况下似乎不起作用。我将如何从该“功能”中获取json?走向全球。
为了方便阅读,我将函数xml2json截短了。
document.addEventListener('DOMContentLoaded', ()=>{
//fetch the data as soon as the page has loaded
let url = "schedule.xml";
fetch(url)
.then(response=>response.text())
.then(data=>{
//console.log(data); //string
let parser = new DOMParser();
let xml = parser.parseFromString(data, "application/xml");
console.log(xml);
function xml2json(xml, tab) {}
json = xml2json(xml);
json = json.replace('undefined','');
json = JSON.parse(json);
console.log(json);
return json;
});
return data;
});
var json = data;
document.write(json);