【发布时间】:2021-04-21 20:41:19
【问题描述】:
我需要使用我的 react 应用程序读取位于我机器上的 JSON 文件,但似乎没有任何效果。
我尝试导入 fs(使用 import 和 require),但它返回一个空白对象,我无法使用它的任何功能。
Jquery 似乎也找不到我的文件。
我搜索的每个地方都使用 FS、Jquery 或 FileReader(最后一个总是用于客户端上传到页面的文件)。
这是我尝试使用 FS 的解决方案之一:
const fs = require("fs");
export default function getJson() {
let rawdata = fs.readFile("file.json");
let json = JSON.parse(rawdata);
console.log(json);
}
当我重新加载浏览器时
TypeError: fs.readFile is not a function
使用 Jquery 我试过这个:
import $ from "jquery";
export default function getJson() {
$.getJSON("file.json", function() {
console.log("success");
});
}
控制台显示:
Failed to load resource: the server responded with a status of 404 (Not Found)
我希望你能帮助我。 提前致谢!
【问题讨论】:
标签: javascript node.js json reactjs