在webview加载本地vue项目时出现Fetch API cannot load file问题。

221 阅读1分钟

在webview加载本地vue项目时出现Fetch API cannot load file问题。

把fetch请求换成XMLHttpRequest

async function readTextFile(filePath: string): Promise<ArrayBuffer> {
  return new Promise((resolve, reject) => {
    const x = new XMLHttpRequest();
    x.responseType = "arraybuffer"
    x.open('GET', filePath, true);
    x.onreadystatechange = function () {
      if (x.readyState === 4) {
        if (x.status >= 200 && x.status < 300) {
          //成功啦
          resolve(x.response);
        } else {
          //如果失败
          reject(x.status);
        }
      }
    }
    x.send(null);
  })}