我正在学习ReactJs,想要显示天气数据。API Url回馈:
{
"response": {
"version":"0.1",
"termsofService":"http://www.wunderground.com/weather/api/d/terms.html",
"features": {
"conditions": 1
}
}
,"current_observation": {
"image": {
"url":"http://icons.wxug.com/graphics/wu2/logo_130x80.png",
"title":"Weather Underground",
"link":"http://www.wunderground.com"
}, etc. etc.已更改为
componentDidMount() {
$.ajax({
url: "http://api.wunderground.com/api/*****/....",
success: function(data){
this.setState({
wetter: data
});
}.bind(this)
});
}
render() {
return <div>{this.state.wetter.response.termsofService}</div>;
}
But only get console: Uncaught TypeError: Cannot read property 'termsofService' of undefined我更改了它,但它看起来仍然logically..but错误
发布于 2016-04-25 02:54:04
不要将wetter设置为data.response,只需将其设置为data即可。
这样,您就可以到达整个对象。例如:
return <div>{this.state.wetter.response.termsofService}</div>;
// http://www.wunderground.com/weather/api/d/terms.html或
return <div>{this.state.wetter.response.features.conditions}</div>;
// 1或
return <div>{this.state.wetter.current_observation.image.url}</div>;
// http://icons.wxug.com/graphics/wu2/logo_130x80.pnghttps://stackoverflow.com/questions/36822819
复制相似问题