javascript - Trying to access response data using fetch -
i'm trying simple make request front end of app using fetch api
let request = new request('http://localhost:3000/add', { headers: new headers({ 'content-type': 'text/json' }), method: 'get' }); fetch(request).then((response) => { console.log(response); });
i handling request on server so,
app.get('/add', (req, res) => { const data = { "number1": "2", "number2": "4" }; res.send(data); });
however, when try access data on front end console.log(response), following object
response {type: "basic", url: "http://localhost:3000/add", redirected: false, status: 200, ok: true…} body:(...) bodyused:false headers:headers ok:true redirected:false status:200 statustext:"ok" type:"basic" url:"http://localhost:3000/add" __proto__:response
the response body empty. assumed that's data show up? how pass data server?
okay, works on front end
fetch(request).then((response) => { console.log(response); response.json().then((data) => { console.log(data); }); });
the key part resolution of promise chain.
similar question here javascript fetch api - why response.json() return promise object (instead of json)?
Comments
Post a Comment