简体   繁体   English

React-从Ajax发布请求的响应的Meta-Data元素中获取“消息”

[英]React - fetch 'message' from Meta-Data element of the response from an Ajax post request

So I have received this response from an Ajax post request (part of the response is shown) where: 因此,我已经收到来自Ajax发布请求的响应(显示了部分响应),其中:

Data:
  Meta-Data:
    Topic : "Sign-Up"
    Message : "Already in the System!"
    Response Code : "650"
.
.
.  

how can I fetch the message and show it to user? 如何获取消息并将其显示给用户? I was thinking I could do this in React: 我当时想我可以在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}) }

and then in render() I do: 然后在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>)}

Which doesn't work at all! 这根本不起作用! "it is a part of my code, I hope I've included all necessary parts" “这是我的代码的一部分,我希望我已经包括了所有必要的部分”

Two things I can see. 我可以看到两件事。 You had this.postResponse. 您有this.postResponse。 It should be this.state.postResponse from what I can tell. 据我所知应该是this.state.postResponse。 Also, you need to put “meta-data” into quotes and use brackets to reference it, as shown below. 另外,您需要将“元数据”放在引号中,并使用方括号来引用它,如下所示。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM