【发布时间】:2017-07-18 03:57:09
【问题描述】:
我有一个问题,我无法使用 axios react js 从 wp API 获取数据 我的代码:
var React = require('react');
var ReactDOM = require('react-dom');
var axios = require('axios');
var Wordpress = React.createClass({
getInitialState: function() {
return {posts: []};
},
componentDidMount() {
axios.get(`http://localhost/scareid/wp-json/wp/v2/posts`).then(res => {
const posts = res.data.map(obj => obj.data);
this.setState({posts});
});
},
render() {
return (
<div>
<h1>{`/r/${this.props.title}`}</h1>
<ul>
{this.state.posts.map(post => <li key={post.id}>{post.title}</li>)}
</ul>
</div>
);
}
})
module.exports = Wordpress;
我的 api
控制台日志数据: enter image description here
【问题讨论】:
-
控制台日志来了吗?
-
按图像控制台日志数据
-
是的,但我看不出错误是什么。你能在这一行下一个断点并检查响应吗: const posts = res.data.map(obj => obj.data);