简体   繁体   English

在node.js中使用res()时会忽略标签

[英]tags being ignored when using res() in node.js

I couldn't find anything about this so I guess I will post it here. 我对此一无所获,所以我想我会在这里发布。 I am a newbie at node.js 我是node.js的新手

I tried creating a button using node js with the following code: 我尝试使用带有以下代码的节点js创建按钮:

response.writeHead(200, {"Content-Type": "text/plain"});
response.end('<!DOCTYPE html><html><head><body><button type="button">Click Me!</button></body></head></html>');

Yet when seen on the browser, it displays: 但是在浏览器上看到时,它显示:

<!DOCTYPE html>
 <html>
  <head>
   <body>
    <button type="button">Click Me!</button>
   </body>
  </head>
 </html>

as if the tags are completely ignored. 就像标记被完全忽略一样。

Inspecting the html source code, it looks like this: 检查html源代码,如下所示:

<html>
<head>
<style type="text/css"></style>
</head>
<body>
<pre style="word-wrap:break-word; white-space: pre-wrap;">
<!DOCTYPE html>
<html>
 <head>
  <body>
   <button type="button">Click Me!</button>
  </body>
 </head>
</html>
</pre>
</body>
</html>

Any Ideas why this is happening? 任何想法为什么会这样? Thanks! 谢谢!

This is happening because you have text/plain inside response.writeHead(200, {"Content-Type": "text/plain"}); 发生这种情况,因为你有text/plain内部response.writeHead(200, {"Content-Type": "text/plain"}); you need to have text/html . 您需要拥有text/html

response.writeHead(200, {"Content-Type": "text/html"});

Using text/plain the response won't be parsed and you get what you give, plain text (or "tags are ignored"). 使用text/plain不会解析响应,您将得到纯文本(或“标签被忽略”)的内容。

Using text/html will tell the browser that this is HTML and it needs to be parsed. 使用text/html会告诉浏览器这是HTML,需要对其进行解析。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM