更新时间:2022-03-10 18:22:27
在 JS 中读取,需要使用 DOMParser
API.下面是一个例子:
To read in JS, you need to use DOMParser
API. Following is an example:
const text = "<string>This is my xml</string>"; //API response in XML
const parser = new DOMParser();
const xmlDOM = parser.parseFromString(text,"text/xml");
const value = xmlDOM.getElementsByTagName("string")[0].childNodes[0].nodeValue;
console.log(value)
使用 fetch()
API 的示例
Example using fetch()
API
fetch('Your_API_URL')
.then(response=>response.text())
.then(data=>{
const parser = new DOMParser();
const xmlDOM = parser.parseFromString(data,"text/xml");
const value = xmlDOM.getElementsByTagName("string")[0].childNodes[0].nodeValue;
console.log(value)
})
.catch(err=>console.log(err))