React Native fetch请求失败

本白在react native中使用fetch进行网络请求的时候显示请求失败。

请求部分代码如下:

fetch('../data/myjson.json')
.then(function(response){
    return response.json();
}).then(function(json){
    console.log('parsed json', json);
}).catch(function(ex){
    console.log('parsing failed')
});

../data/myjson.json是我的本地json文件

Thanks for all helps

阅读 5.5k
2 个回答

fetch 只能请求网络的吧

自己找到了需要的答案。

因为react native 本身会在目录根路径搭建了服务器,所以我直接
fetch('http://localhost:8081/data/myjson.json')
就好了。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题