小编hud*_*dit的帖子

使用'url'属性时,ReactJS不从服务器获取数据

我是ReactJS的初学者,刚开始使用React官方网站上的示例代码.当我在"从服务器中获取"一节中尝试代码时,我无法让它工作.

我试过两条相对路径

React.render(
  <CommentBox url="../data/data.json" />,
  document.getElementById('content')
);
Run Code Online (Sandbox Code Playgroud)

和绝对的道路

React.render(
  <CommentBox url="http://localhost/path/to/data.json" />,
  document.getElementById('content')
);
Run Code Online (Sandbox Code Playgroud)

但他们都没有正确运行.当我在Chrome开发工具中查看网络面板时,我看到该页面甚至没有发送请求data.json.因此我得到了一个错误Cannot read property 'comments' of undefined.

更多代码:

var Comment = React.createClass({
  render: function() {
    return (
      <div className="comment">
        from {this.props.author} <br/>
        {this.props.children}
      </div>
    );
  }
});

var CommentList = React.createClass({
  render: function() {
    var commentNodes = this.props.comments.map(function(comment){
      return (
        <Comment author={comment.author}>
          {comment.text}
        </Comment>
      );
    });
    return (
    <div className="comment-list">
      {commentNodes}
    </div>
    );
  }
});

var …
Run Code Online (Sandbox Code Playgroud)

javascript reactjs

13
推荐指数
2
解决办法
4971
查看次数

标签 统计

javascript ×1

reactjs ×1