[英]React - fetch 'message' from Meta-Data element of the response from an Ajax post request
因此,我已經收到來自Ajax發布請求的響應(顯示了部分響應),其中:
Data:
Meta-Data:
Topic : "Sign-Up"
Message : "Already in the System!"
Response Code : "650"
.
.
.
如何獲取消息並將其顯示給用戶? 我當時想我可以在React中做到這一點:
state = {
username : "John",
password : "somePass",
postResponse : null
}
axios.post(URL, JSON.stringify({ username : this.state.username , password: this.state.password})
.then(response => { this.setState({postResponse : response.data}) }
然后在render()中執行:
render(){
let message = null;
if (this.state.postResponse){
message = <p>{this.postResponse.data.meta-data.message}</p> //apparently the dash is breaking the code!
}
return(<div> {message} </div>)}
這根本不起作用! “這是我的代碼的一部分,我希望我已經包括了所有必要的部分”
我可以看到兩件事。 您有this.postResponse。 據我所知應該是this.state.postResponse。 另外,您需要將“元數據”放在引號中,並使用方括號來引用它,如下所示。
state = {
username : "John",
password : "somePass",
postResponse : null
}
axios.post(URL, JSON.stringify({ username : this.state.username , password: this.state.password})
.then(response => { this.setState({postResponse : response.data}) }
render(){
let message = null;
if (this.state.postResponse){
message = <p>{this.state.postResponse[“meta-data”].message}</p>
}
return(<div> {message} </div>)}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.