2017-07-28 68 views
2

我想簡單的東西,我用像這樣嘗試使用取

let request = new Request('http://localhost:3000/add', { 
    headers: new Headers({ 
     'Content-Type': 'text/json' 
    }), 
    method: 'GET' 
}); 

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

我喜歡處理在服務器上的這個請求的提取API使從我的應用程序的前端的請求響應數據訪問所以,

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 

響應正文爲空。我認爲這是數據顯示的位置?如何有效地從服務器傳遞數據?

+0

嘗試改變'res.send(數據);''來res.end(數據);'? – Val

回答