Javascript读取文件


                    

当我在 Json模式中获得“ $ ref”时,我必须动态读取JSON文件。</ p>

我尝试使用node.js'fs'模块访问json文件。 但这会导致错误'fs.readFile()'</ strong>不起作用。</ p>

我后来尝试了Ajax调用,但出现以下错误。</ p>

  XMLHttpRequest无法加载文件:///Users/as6/Downloads/1099K.json。 跨源请求仅支持以下协议方案:http,数据,chrome,chrome扩展名,https。
</ code> </ pre>

下面是我的代码,可用来读取javascript中的json文件。</ p>

 函数readTextFile(文件,回调){
     var rawFile = new XMLHttpRequest();
     rawFile.overrideMimeType(“ application / json”);
     rawFile.open(“ GET”,file,true);
     rawFile.onreadystatechange = function(){
         如果(rawFile.readyState === 4 && rawFile.status ==“ 200”){
             callback(rawFile.responseText);
         }
     }
     rawFile.send(null);
}
readTextFile(“ file:///Users/as6/Downloads/1099K.json”,function(text){
       var data = JSON.parse(text);
       console.log(data);
   });
</ code> </ pre>

如何在没有 Jquery </ strong>的情况下使用javascript读取JSON文件?</ p>
     </ div>

展开原文

原文

I have to read JSON file dynamically when I get "$ref" in Json schema.

I have tried accessing json file using node.js 'fs' module. But that gives error 'fs.readFile()' is not function.

I have later tried the Ajax call but I got following error.

XMLHttpRequest cannot load file:///Users/as6/Downloads/1099K.json. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.

Below is my code to read json file in javascript.

function readTextFile(file, callback) {
    var rawFile = new XMLHttpRequest();
    rawFile.overrideMimeType("application/json");
    rawFile.open("GET", file, true);
    rawFile.onreadystatechange = function() {
        if (rawFile.readyState === 4 && rawFile.status == "200") {
            callback(rawFile.responseText);
        }
    }
    rawFile.send(null);
}
readTextFile("file:///Users/as6/Downloads/1099K.json", function(text){
      var data = JSON.parse(text);
      console.log(data);
  });

How will I be able to read JSON file in javascript without Jquery?

2个回答


该错误已经告诉您出了什么问题。
跨源请求仅支持以下协议方案:http,数据,chrome,chrome扩展名,https。</ code> </ p>

问题是您正在尝试通过 file:// </ code>协议打开本地文件,并且正如浏览器告诉您的那样,您无法XHR使用它。 简单的解决方法是在计算机上安装最小的静态文件服务器。</ p>

此处提供了有关COR的更多信息: https:// developer。 mozilla.org/zh-CN/docs/Web/HTTP/Access_control_CORS </ p>

正如您提到的nodejs一样,我将继续进行安装。 我建议 https://www.npmjs.com/package/http-server 这样 您可以启动并运行。</ p>
     </ div>

展开原文

原文

The error is already telling you what's wrong. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.

The thing is you are trying to open a local file via the file:// protocol and as the browser is telling you, you can't XHR using that. The easy fix here would be to install a minimal static file server on your computer.

Some more information about CORs here: https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS

As you mentioned nodejs, I will go ahead and assume you have it installed. I suggest https://www.npmjs.com/package/http-server so you can get up and running.


您需要在该目录中运行服务器,而不仅仅是在浏览器中打开html。
尝试在html文件所在的目录中运行 python -m SimpleHTTPServer </ code>。
将json文件移动到html文件所在的位置。 然后将文件:///Users/as6/Downloads/1099K.json替换为 http:// localhost:8000 / 1099K .json </ p>

浏览器这种方式很奇怪,它不会让您对以file:/// </ p>开头的链接进行ajax
     </ div>

展开原文

原文

you need to run a server in that directory, not just open your html in a browser. Try running python -m SimpleHTTPServer in the directory where your html file is. Move your json file where your html file is. Then replace file:///Users/as6/Downloads/1099K.json with http://localhost:8000/1099K.json

Browser is weird this way, it won't let you do ajax for links starting with file:///

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐