发送内容类型的JSON而不是渲染页面

发布于 2025-02-08 04:30:08 字数 1484 浏览 0 评论 0 原文

我有此代码:

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
  }
]

我不确定为什么会发生这种情况。有人可以分享他们认为可能是问题吗?

I have this code:

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})
    }
})
})

what happens is, when I render doing this:

res.render('dashboard.ejs', {data: req.data, body: body, amountOfTokens: amountOfTokens, numOfRecords: numOfRecords})

on the front-end, it somehow returns content-type: json header and json content
and the browser isn't rendering the page, just the shows JSON header and the path to what the page that were supposed to be going to.

and body, which returns all the users records, looks like this:

[
  { 
    id: '912342930198368362323',
    user: '[email protected]',
    name: 'hello world',
    date: 1655400772,
    input: [ [Object], [Object] ],
    record_type: 'type1',
    output: 'hey',
    tokens: 43
  }
]

im not sure why this is happening. could someone share what they think could be the problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情愿 2025-02-15 04:30:08

使用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/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文