【发布时间】:2017-10-23 15:45:53
【问题描述】:
我有 symfony rest api,我正在为此编写一些前端(第一次使用 react)。所以,这是我的 index.js:
import React, {Component} from 'react';
import ReactDOM from 'react-dom';
import axios from 'axios';
class App extends Component {
constructor(props) {
super(props);
this.state = {
visits: []
};
}
componentDidMount() {
const url = 'http://localhost/app.php/api/visits?from=2017-05-05&to=2017-05-08';
axios.get(url)
.then(response => response.json())
.then(
visits => this.setState({visits})
).catch(err => err)
}
render() {
return (
<div>
<ul>
{this.state.visits.map((visit, index) => <li key={index}>{visit}</li>)}
</ul>
</div>
)
}
}
const app = document.getElementById('app');
ReactDOM.render(<App/>, app);
来自 api 的响应如下:
{
"2017-05-05": 2,
"2017-05-06": 8,
"2017-05-07": 10,
"2017-05-08": 1,
}
我如何遍历它并将其打印到屏幕上? 现在我没有收到任何错误,只是从服务器获得响应,但屏幕上没有任何内容。
问候
【问题讨论】: