简体   繁体   English

从公共 json 文件而不是 REST Z8A5DA52ED126447D359E70C05721A8 获取 json 数据

[英]get json data from a public json file not REST api with React

I have a url similar to https://www.nonexistentsite.com/fubar.json where fubar.json is a public json file which will download to your file system if you navigate to the url with your browser. I have a url similar to https://www.nonexistentsite.com/fubar.json where fubar.json is a public json file which will download to your file system if you navigate to the url with your browser. I have a React web app where I want to read that file directly so as to display some of its data.我有一个 React web 应用程序,我想直接读取该文件以显示其中的一些数据。 I don't want to bother with any kind of a backend that would download the file to the apps file system so that it can read it.我不想打扰任何将文件下载到应用程序文件系统以便它可以读取它的后端。 I want the React front end to read it directly in it's client side code with a fetch or an axios call or something like that.我希望 React 前端通过 fetch 或 axios 调用或类似的方式直接在其客户端代码中读取它。 I'm familiar with the typical situation where I have a REST url like https://www.nonexistentsite.com/fubar which I can call and get the data.我熟悉我有一个 REST url 之类的典型情况,例如https://www.nonexistentsite.com/fubar并获取我可以调用的数据。 I'm failing to find info on how to handle this situation.我找不到有关如何处理这种情况的信息。

You could use Axios to load the data from the json file.您可以使用Axios从 json 文件加载数据。

Example usage;示例用法;

axios.get('https://www.nonexistentsite.com/fubar.json')
.then(jsoncontent => {
  console.log(jsoncontent);
  //do stuff with jsoncontent here
});

Maybe I'm misunderstanding your question, but I believe if you are just needing to fetch the json from a hosted file, you should be able to do so with axios.get(url)也许我误解了你的问题,但我相信如果你只需要从托管文件中获取 json,你应该可以使用 axios.get(url)

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

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