2016-08-10 57 views
0

我有简单的JSONReactJs呈现简单的JSON

{"id":157,"content":"Hello, World!"} 

我想渲染ID在anotther一个DIV和内容。对我来说问题是当我打电话{this.state.data.content}两次崩溃。

var Stuff = React.createClass({ 
    getInitialState: function() { 
     return { 
      data: [] 
     }; 
    }, 
    componentDidMount: function() { 
     $.ajax({ 
      url: "http://rest-service.guides.spring.io/greeting", 
      dataType: 'json', 
      cache: false, 
      success: function(response) { 
      this.setState({ 
       data: response 
      }); 
      }.bind(this), 
      error: function(xhr, status, err) { 
      console.error(this.props.url, status, err.toString()); 
      }.bind(this) 
     }); 
    }, 
    render: function() { 
     return (
      <div>Response - {this.state.data.content}</div> 
     ); 
    } 
}); 
+0

你有没有[检查你的控制台](http://stackoverflow.com/documentation/javascript/185/hello-world/714/using-console-log)的错误? –

+0

我不知道你在说什么 – TeodorKolev

回答

1

通过周围JSX与另一个DIV

return (
    <div> 
     <div>Response - {this.state.data.content}</div> 
     <div>id - {this.state.data.id}</div> 
     </div> 
    ); 
0

很好解决的问题,尽量呈现字符串,而不是一个对象的

render: function() { 
    return (<div> Whatever - {JSON.stringify(this.state.data)}</div>) 
} 

蒂普:

,如果你想使它漂亮:使用JSON.stringify(this.state.data, null, 2)