javascript - How fetch json data in React -
i new in react , i'm learning on official website tutorial help. so, have structure in app:
-js -notes.js -comment.json index.html
notes.js have next code:
var commentbox = react.createclass({ loadcommentsfromserver: function() { $.ajax({ url: this.props.url, datatype: 'json', cache: false, success: function(data) { this.setstate({data: data}); }.bind(this), error: function(xhr, status, err) { console.error(this.props.url, status, err.tostring()); }.bind(this) }); }, getinitialstate: function() { return {data: []}; }, componentdidmount: function () { this.loadcommentsfromserver(); setinterval(this.loadcommentsfromserver, this.props.pollinterval); }, render: function(){ return ( <div classname="commentbox"> <h1>comments</h1> <commentlist data={this.state.data} /> <commentform /> </div> ); } }); var commentlist = react.createclass({ render: function(){ var commentnodes = this.props.data.map(function (comment) { return ( <comment author={comment.author} key={comment.id}> {comment.text} </comment> ); }); return ( <div classname="commentlist"> {commentnodes} </div> ); } }); var commentform = react.createclass({ render: function () { return ( <div classname="commentform"> hello, world! commentform. </div> ); } }); var comment = react.createclass({ render: function () { return ( <div classname="comment"> <h2 classname="commentauthor"> {this.props.author} </h2> <p> {this.props.children} </p> </div> ); } }); reactdom.render( <commentbox url="comment.json" pollinterval={2000} />, document.getelementbyid('content') );
here file data "comment.json":
[ {"id": "1", "author": "pete hunt", "text": "this 1 comment"}, {"id": "2", "author": "jordan walke", "text": "this *another* comment"} ]
but doesn't work , in console have next message:
jquery.js:9392 http://helper.com/views/com.json?_=1471986105038 404 (not found)
and one:
notes.js:22 com.json error not found
maybe write wrong url parametr in:
<commentbox url="comment.json" pollinterval={2000} />
can me?
Comments
Post a Comment