json - componentDidMount() 获取本地数据
问题描述
我正在尝试使用存储在项目目录中的 JSON 文件来测试 React 组件。
我不确定我是否以正确的方式进行此操作:
constructor(props){
super(props);
this.state = {
videoPath: video,
data: null
}
}
componentDidMount(){
fetch('../../data/jsonfile.json')
.then(response => response.json())
.then(data => this.setState({data}));
}
我收到一条错误消息:
Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0
JSON 文件开始:
{
"0":{
"coords":[
[
645.0,
353.0,
0.9010798558592796,
0.0,
0.0
],
所以里面应该有一个<
。但是当我在 Chrome 中检查源代码时,jsonfile.json 看起来就像一个 HTML 文件。我在用着create-react-app
解决方案
推荐阅读
- python - 可以在没有 await 或 yield 的情况下实现 Python 协程吗?
- xamarin - 如何在 Xamarin 中更改 iOS 中导航栏的对齐方式?
- java - Base64.encode 返回奇怪的字符串
- android - 来自 Google Play 的关于您的权限声明的通知
- spring-batch - Spring批处理绝对路径
- jquery - keydown 事件未绑定到动态 select2 元素
- c++ - 在多播网络中发送和接收 OpenCV Mat 对象
- php - 谷歌的建议随机 php
- python - 插入排序算法因一个错误而关闭
- python - 无法从 Redshift 读取列名有空格的数据