如何使用 react js 和 axios 从 wordpress 获取 API 调用?

How to get API call from wordpress with react js and axios?

我有问题,我无法使用 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

http://pastebin.com/9x4rgJmE

控制台日志数据: enter image description here

好的,找到了。

根本不需要map这一步,数组已经在res中返回了。

替换:

const posts = res.data.map(obj => obj.data);
            this.setState({posts});

与:

if (res && res.data) {
 this.setState({
  posts: res.data 
 });
}