Skip to content

Fetch

fetch 读取 xml 文件

js
async function fetchAndEncodeXML() {
  try {
    // 假设你的 XML 文件位于 public 目录下,路径为 /public/data.xml
    const response = await fetch('/data.xml')
    if (!response.ok) {
      throw new Error('Network response was not ok')
    }
    let xmlContent = await response.text()
    // 去除空格和换行符
    xmlContent = xmlContent.replace(/\s+/g, ' ').trim()
    this.encodedXML = encodeURIComponent(xmlContent)
  }
  catch (error) {
    console.error('Error fetching XML file:', error)
  }
}

基于 MIT 许可发布