【问题标题】:Getting double quotes when returning html返回html时获取双引号
【发布时间】:2019-08-27 14:58:30
【问题描述】:

我有以下途径可以返回我的应用的隐私政策。

const html = `<html><body><div>dsfdsfsfdsfsfsdfdsf</div></body></html>`
const handler = (request, reply) => {
  try {
    return reply(html)
  } catch (err) {
    return reply({ success: false, message: err.message, data: [] })
  }
}

const routeConfig = {
  method: 'GET',
  path: '/privacy-policy',
  config: {
    description: 'Creates a task.',
    notes: ['On success, returns { "data": [ { "tasks" } ]}'],
    handler
  }
}

但是,当我返回 html 内容时,我在内容的开头和结尾都得到了双引号。

如何删除它们?谢谢

【问题讨论】:

  • @saikatchakrabortty 但是我可以在哪里添加该选项?
  • 解决方案中我的 html 内容在哪里?
  • @saikatchakrabortty 仍然得到双引号,伙计
  • @GabrielBleu 非常感谢。但它的快乐。我有回复功能如何添加该选项。

标签: node.js serverless-framework hapijs serverless hapi


【解决方案1】:

你可以在这里做两件事 -

// 1. You can reply with html as json`
return reply({html});

// This will this html in a json and you can use this in frontend receiving a json response 
// 2. You can send the html (file) as a html response
return reply.view('htmlFile.html');
//If you want to render the html

【讨论】:

    【解决方案2】:

    您在html 的开头和结尾得到引号,因为您将其返回为string。您可以做的是,而不是返回字符串中的replay,您可以返回 Html。另一件事是不要忘记响应标头Content-Typetext/html。一旦你设置了响应头,客户端浏览器就会理解响应并开始解析它。

    【讨论】:

      【解决方案3】:

      我想你只是忘了将Content-Type 设置为text/html

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-13
        • 2018-11-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多