我正在尝试从xml Api中提取数据,但控制台未给出任何数据, 我的Javascript代码外观:
var str;
var xhr = new XMLHttpRequest;
xhr.open('GET', 'https://iliass-jobs.personio.de/xml?language=de');
// If specified, responseType must be empty string or "document"
xhr.responseType = 'document';
// Force the response to be parsed as XML
xhr.overrideMimeType('text/xml');
xhr.onload = function () {
if (xhr.readyState === xhr.DONE && xhr.status === 200) {
str = xhr.response;
}
};
xhr.send();
const doc = new DOMParser().parseFromString(str, 'text/html');
const searchId = "126072";
const filteredIdEle = Array.prototype.find.call(doc.body.querySelectorAll('*'), ((ele) => {
return ele.textContent == searchId;
}));
if(filteredIdEle) {
filteredIdEle.parentElement.querySelectorAll('jobDescription').forEach((ele) {
const name = ele.querySelector('name').textContent;
const val = ele.querySelector('value').textContent;
console.log("Name :", name);
console.log("Value :", val);
});
}
但是当我直接将XML内容存储在Variable str内部时,它工作得很好。