【问题标题】:getJson error "Not allowed to load local"getJson 错误“不允许加载本地”
【发布时间】:2021-06-05 01:23:12
【问题描述】:

我在本地有 json 文件,例如“D:\ABC\JsonFile\file.json”,我需要读取该文件中的数据。当我运行时出现错误“不允许加载本地”我的代码有什么问题?

这是我的代码。

     $("#GetJsonData").click(function () {
        $.getJSON("file://D:/ABC/JsonFile/jsondata.json", function (result) {
            console.log(result.ContentName);
        }).fail(function (jqXHR, txtStatus, errThrown) {
            console.log("Status : " + txtStatus);
            console.log("Error : " + errThrown);
        });
    });

【问题讨论】:

标签: c# jquery asp.net-mvc visual-studio asp.net-core


【解决方案1】:

我假设这是客户端 JS。

由于浏览器应被视为“沙盒”,因此您无权直接访问驱动器。您只能使用 URI 通过 HTTP 访问其他服务器上的文件。

为此,您需要托管文件并使用“https://yourserver.com/JsonFile/jsondata.json”

【讨论】:

  • 这个是正确的,你只能通过http服务器访问文件,不能在本地访问,因为你设置了OP。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-03
  • 1970-01-01
  • 2015-12-07
  • 1970-01-01
  • 2016-10-08
相关资源
最近更新 更多