Javascript Node express API未得到响应?
我已经创建了一个node express APIJavascript Node express API未得到响应?,javascript,node.js,express,fetch,Javascript,Node.js,Express,Fetch,我已经创建了一个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]
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);
})
我请求像这样使用fetch
fetch('http://localhost:5050/api/getData').then(response => {
console.log(response)
}).catch(err => {
console.error('Network error', err)
})
在网络选项卡中,我从服务器发送了相同的JSON对象。在resonpse的控制台日志中,它打印以下内容
如何使用从服务器发送的JSON数据?要获取实际数据,您需要像这样的
response.JSON()
来实际读取和解析响应体:
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)
});
你复习过你的功课了吗?您对其中的任何内容感到困惑吗?
。然后(response=>response.json())。然后(data=>console.log(data))
您可以使用console.log()打印数据,或者您可以使用json.stringify()将json格式的数据转换为字符串并在控制台上显示。请记住,在前端获取的数据将显示在浏览器控制台上,而不是后端/命令控制台上。