如何使用axios获取本地文件?

时间:2018-03-12 12:47:05

标签: react-native axios

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

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)'

enter image description here

该文件存在于路径

我的文件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"}]

1 个答案:

答案 0 :(得分:2)

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

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

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