原生get请求读取本地json文件,electron vue

 1 readLocalFile(fileUrl) {
 2       let xhr = null
 3       if (window.XMLHttpRequest) {
 4         xhr = new XMLHttpRequest()
 5       } else {
 6         // eslint-disable-next-line
 7         xhr = new ActiveXObject('Microsoft.XMLHTTP')
 8       }
 9       const okStatus = document.location.protocol === 'file' ? 0 : 200
10       xhr.open('GET', fileUrl, false)
11       xhr.overrideMimeType('text/html;charset=utf-8')
12       xhr.send(null)
13       return (xhr.status === okStatus ? xhr.responseText : null)
14 
15 },
 // 读取本地配置文件
      const readFile = JSON.parse(this.readLocalFile('文件路径'))
a = $.ajax({
   url: "a123.json",//json文件位置,文件名
   type: "GET",//请求方式为get
   dataType: "json", //返回数据格式为json
   async: false,
   success: function (data) {//请求成功完成后要执行的方法 
    console.log(data);
   }
  });//jquery方法

 

posted on 2022-08-17 14:29  阿术阿术  阅读(521)  评论(0)    收藏  举报