【发布时间】:2017-11-21 20:57:15
【问题描述】:
这可能是一个非常愚蠢的问题,但我在网上找不到任何解决方案,请不要抨击我。我设置了一个 create-react-app 并想在表格中显示一些数据。
这是我的 JSON:
[ {
"unitid" : 29,
"createddate" : 1510324778000,
"latitude" : 49.402008056640625,
"longitude" : 11.901968002319336,
"senderid" : 6,
"signalstrength" : 37
}, {
"unitid" : 34,
"createddate" : 1510563384000,
"latitude" : 49.22679901123047,
"longitude" : 12.845210075378418,
"senderid" : 8,
"signalstrength" : 0
},......
我的表格需要一个包含所有属性的 JSON 数组才能正确显示每一列。
这是我尝试从我的端点获取数据的代码:
class App extends Component {
constructor(props){
super(props);
this.state = {
vehicleList: [],
};
}
componentDidMount(){
fetch('endpoint-link', {mode: 'no-cors', method: 'get'})
.then(response => response.json())
.then(result => {
this.setState({vehicleList: result.vehicleList})
console.log(result)
})
.catch(error => {
this.setState({isLoaded: true, error})
console.log(error)
});
}
render() {
const {vehicleList} = this.state;
console.log(vehicleList);.......
当我在 Chrome 中打开 Devtools 时,选择“网络”,我可以看到端点已知并且数据已找到,但不知何故 fetch 方法没有将 JSON 加载到数组中。
【问题讨论】:
-
在“这是我的 JSON”下,你只有一个数组。它没有
vehicleList属性。然而,您正在将result.vehicleList放入您的状态。 JSON 是否有vehicleList属性,还是没有? -
不,它没有。你会为此推荐什么?
标签: javascript json reactjs fetch