【问题标题】:How to get a local file using axios?如何使用 axios 获取本地文件?
【发布时间】:2018-08-20 11:55:11
【问题描述】:

我正在尝试获取本地文件:

Resultado.js:

componentWillMount(){
  axios.get('../config/db.json')
  .then(function(response){
    alert('ok');
  })
  .catch(function(error){
    alert('error');
  });
}

但是,我收到一个错误:

尝试在空对象引用上调用虚拟方法“boolean java.lang.String.equals(java.lang.Object)”

文件存在于路径中

我的文件 db.json:

[{"id":"87","codigo":"00504.050.000.00.00","nomeCadastro":"GOIVO DOBRADO"},{"id":"86","codigo":"02023.050.000.10.02","nomeCadastro":"GERB ONEDIN"},{"id":"85","codigo":"00212.070.999.00.00","nomeCadastro":"CRIS SANT REAN"},{"id":"84","codigo":"01297.019.000.00.00","nomeCadastro":"ANTH VARIADO"}]

【问题讨论】:

  • axios 用于 HTTP 方法。在您的情况下,只需导入您的数据并使用它import data from '../config/db.json'
  • 以上评论是正确的。你也可以cd 进入配置目录并运行python -m SimpleHTTPServer 8000 - 然后调用http://localhost:8000/db.json
  • @TuấnTrầnDuy 如何使用数据。你能写一个例子吗?
  • @ItaloRodrigo 你可以console.log 它在任意函数中查看:))
  • @TuấnTrầnDuy 我编辑问题并添加我的 json 文件

标签: react-native axios


【解决方案1】:

只需从json 文件中导入数据并使用它

    import data from '../config/db.json';

    render(){
     if(data){
      // do whatever you want with data here, 
     }
     ....
    }

【讨论】:

  • 如果是静态HTML,导入不行怎么办
猜你喜欢
  • 1970-01-01
  • 2018-02-21
  • 2019-03-05
  • 2019-08-02
  • 1970-01-01
  • 1970-01-01
  • 2022-12-17
  • 1970-01-01
  • 2017-06-05
相关资源
最近更新 更多