2017-11-24 3 views
0

私はちょっと反応して開発中にかなり新しいです。レスポンスの値を取得する方法ReactJs

私はコンソールが未定義の私を示して私のJSONレスポンスから

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" 
    } 
    ] 
} 

をデータをフェッチするためにいくつかの問題を得ました。 なぜプレーンjs経由でデータを取得できないのでしょうか?

+0

uは、応答を確認しましたか?最初にレスポンスを確認し、そのオブジェクトにアクセスしてみてください。 –

+0

私はすでに応答を確認しました。コンソールに有効なjson! –

+0

jsonを送信できますか? –

答えて

0

だけでなく、あなたが最初にそれをparseする必要がありますのでaxiosコールの応答は、文字列であるので、

axios.get('http://localhost:9000/test') 
    .then(function (response) { 
     const parsedResponse = JSON.parse(response); 
     console.log(parsedResponse.name)); 
    }); 
-1

あなたがしていることを確認そのない

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

本当に変更が加えられたことに気づいて、より多くのエラーが予想されます –

関連する問題