尝试使用提取访问响应数据

我尝试了一些简单的地方,我使用获取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); }); 

我在服务器上处理这个请求是这样的,

 app.get('/add', (req, res) => { const data = { "number1": "2", "number2": "4" }; res.send(data); }); 

但是,当我尝试访问前端console.log(响应)上的数据时,我得到以下对象

 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 

响应主体是空的。 我认为这是数据显示的位置? 如何有效地从服务器传递数据?

好的,这在我的前端工作

 fetch(request).then((response) => { console.log(response); response.json().then((data) => { console.log(data); }); }); 

关键部分是承诺链的解决。

类似的问题在这里JavaScript获取API – 为什么response.json()返回一个promise对象(而不是JSON)?