发送内容类型的JSON而不是渲染页面
我有此代码:
router.get('/findRecord', authCheck, (req, res) => {
let amountOfTokens = req.session.amountOfTokens
request.post({
url: 'https://myurl.com/user_records',
headers: {
"Cookie": req.headers?.cookie,
},
json: {
max_date: new Date().valueOf()
}
}, async function(err, httpResponse, body) {
// console.log(body)
if (err) {
console.log(err)
} else {
if (amountOfTokens == undefined || amountOfTokens == null) {
res.redirect('/dashboard')
}
let numOfRecords = body.length
res.render('dashboard.ejs', {data: req.data, body: body, amountOfTokens: amountOfTokens, numOfRecords: numOfRecords})
}
})
})
发生的情况是,当我渲染这样做时:
res.render('dashboard.ejs', {data: req.data, body: body, amountOfTokens: amountOfTokens, numOfRecords: numOfRecords})
在前端,它以某种方式返回 content-type:json header
和 json content
而且浏览器没有呈现页面,只是显示 JSON标头
以及通往本应要使用的页面的路径。
Body
返回所有用户的记录,看起来像这样:
[
{
id: '912342930198368362323',
user: '[email protected]',
name: 'hello world',
date: 1655400772,
input: [ [Object], [Object] ],
record_type: 'type1',
output: 'hey',
tokens: 43
}
]
我不确定为什么会发生这种情况。有人可以分享他们认为可能是问题吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用res.send()显示json而不是res.render()
更多信息:
use res.send() to display JSON instead of res.render()
more info here:
https://www.geeksforgeeks.org/express-js-res-send-function/