I have created a node-express API.
router.get('/getData', function(req, res) { let data = { title: 'Message Effectiveness – Bar Chart – 1Q', chartData: [ { title: 'Motivating', data: [.75, .45, .45, .41, .37, .35, .30, .20, .20] }, { title: 'Believable', data: [.75, .45, .45, .41, .37, .35, .30, .20, .20] }, { title: 'Differentiating', data: [.75, .45, .45, .41, .37, .35, .30, .20, .20] }, { title: 'Effectiveness ^', data: [.75, .45, .45, .41, .37, .35, .30, .20, .20] }, ] } res.json(data); })
And I request using fetch like this
fetch('http://localhost:5050/api/getData').then(response => { console.log(response) }).catch(err => { console.error('Network error', err) })
In Network tab I got the same JSON object send from server. In console log of resonpse it print below things….
How can I use the JSON data that send from the server?
Advertisement
Answer
To get the actual data, you need response.json()
like this to actually read and parse the body of the response:
fetch('http://localhost:5050/api/getData').then(response => { return response.json(); }).then(data => { // use the data here console.log(data); }).catch(err => { console.error('Network error', err) });