Getting double quotes when returning html

我怕爱的太早我们不能终老 提交于 2019-12-11 15:54:23

问题


I have below route to return privacy policy for my app.

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

However when I am returning the html content I am getting double quotes at the start and at the end of my content.

How can I remove them? Thank you


回答1:


You can do two things here -

// 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:


You are getting quotes in start and end of the html because of you are returning it as string. What you can do is, rather returning the replay in the string you can return the Html. One more thing is not to forget the response header Content-Type as text/html. once you set the response header, the client browser will understand the response and start parsing it.




回答3:


I think you just forgot to set Content-Type to text/html.



来源:https://stackoverflow.com/questions/57585157/getting-double-quotes-when-returning-html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!