我有一个React组件,它从后端加载JSON并以表格格式显示它们。
(k, v)
以下是REST API响应:
var App = React.createClass({
loadMoviesFromServer: function () {
var self = this;
$.ajax({
url: "http://localhost:9000/movies"
}).then(function (data) {
console.log(data);
self.setState({movies: data._embedded.movies});
});
},
getInitialState: function () {
return {movies: []};
},
componentDidMount: function () {
this.loadMoviesFromServer();
},
render() {
return ( <MovieTable movies={this.state.movies}/> );
}
});
ReactDOM.render(<App />, document.getElementById('root') );
</script>
然而我收到错误:
[{"id":1,"name":"Sholay","day":"Monday","description":"Action"},{"id":2,"name":"Tomb Raider","day":"Monday","description":"Action"},{"id":3,"name":"Toby Maguire","day":"Tuesday","description":"Action"}]
我应该重新格式化JSON吗?或者我应该更改Jquery中的预期元素。
感谢
答案 0 :(得分:2)
如果您控制日志记录响应data
,则此处没有字段_embedded
。
尝试从setState调用中删除_embedded
和movies
键:
self.setState({movies: data});