简体   繁体   English

编织到 HTML 文档并在服务器上运行时出现“不允许加载本地资源”错误

[英]"Not allowed to load local resource" error when knitting to HTML document and running on a server

I'm trying to load a local JSON file in an RMarkdown (knitting to HMTL):我正在尝试在 RMarkdown (编织到 HMTL)中加载本地 JSON 文件:

```{js}

  $(document).ready(function(){
        $.getJSON("C:/Users/Data/my_json.json", function(data){
            console.log(data); 
        }).fail(function(){
            console.log("An error has occurred.");
        });
    });

```

So, I'm aware that I can't serve local files on chrome without running a server.所以,我知道我不能在不运行服务器的情况下在 chrome 上提供本地文件。 So, I dropped the HMTL output from the RMarkdown into Prepros, which comes with a built-in HTTP & HTTPS server.所以,我将 HMTL output 从 RMarkdown 放到 Prepros 中,它带有内置的 HTTP 和 HTTPS 服务器。 It's running at http://localhost:8848/ And still I get the errors:它在http://localhost:8848/运行,但我仍然收到错误:

Not allowed to load local resource 

and

Failed to load resource: the server responded with a status of 404 (Not Found)

Can anyone tell me how I can fix this?谁能告诉我如何解决这个问题? I assumed I could run local json files via Prepros, since it's spinning up a server.我假设我可以通过 Prepros 运行本地 json 文件,因为它正在启动服务器。 Thanks.谢谢。

The browser wont let you load a file from the host - that would be a huge security issue.浏览器不允许您从主机加载文件——这将是一个巨大的安全问题。

You need to make it so that the server has an endpoint that returns the JSON, and then put in the url for your getJSON call, eg:您需要使服务器具有返回 JSON 的端点,然后为您的 getJSON 调用放入 url,例如:

$(document).ready(function(){
    $.getJSON("http://localhost:8848/path/to/my_json.json", function(data){
        console.log(data); 
    }).fail(function(){
        console.log("An error has occurred.");
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM