Max Günther Max Günther - 9 months ago 101
React JSX Question

How to get Values of Response ReactJs

Hey guys im pretty new in developing with react.

I got some problems to fetch data from my json response

axios.get('http://localhost:9000/test')
.then(function (response) {
console.log(response.data.name);
console.log(response.name)
});





{
"name": "mkyong",
"age": 30,
"address": {
"streetAddress": "88 8nd Street",
"city": "New York"
},
"phoneNumber": [
{
"type": "home",
"number": "111 111-1111"
},
{
"type": "fax",
"number": "222 222-2222"
}
]
}


The console shows me undefined.
Whats the problem why i cant get the data via plain js?

Answer Source

well because the response of an axios call is a string, so you have to parse it first,

axios.get('http://localhost:9000/test')
   .then(function (response) {
         const parsedResponse = JSON.parse(response);
         console.log(parsedResponse.name));
     });
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download