1
我已經導入axios
庫和React
。我在React
分量上做get調用,請求是正確的,也返回響應,它給了我這樣的:GET請求在React Component中不起作用
{"functionality": [
{"category": "", "price": 2.0, "moltiplicator": 100.0, "name": "Plat"}
]
}
但是當我設置響應狀態變量,它不工作,這是代碼:
export class FetchDemo extends React.Component {
constructor(props) {
super(props);
this.state = {
posts: []
};
}
componentDidMount() {
axios.get(`http://127.0.0.1:8080/get_platforms`)
.then(res => {
this.setState({
posts: res.functionality
});
});
}
render() {
return (
<div>
<ul>
{this.state.posts.map(post =>
<li >{post.category}</li>
)}
</ul>
</div>
);
}
}
來自'axios.get('http://127.0.0.1:8080/get_platforms')'的響應是JSON字符串還是JS對象? –
你確定關於console.llog(res.functionality)? 請將res放置在payphesis axios.get('http://127.0.0.1:8080/get_platforms').then((res)=> {...} – alireza
是一個json字符串 –