繁体   English   中英

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

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

我对此一无所获,所以我想我会在这里发布。 我是node.js的新手

我尝试使用带有以下代码的节点js创建按钮:

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

但是在浏览器上看到时,它显示:

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

就像标记被完全忽略一样。

检查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>

任何想法为什么会这样? 谢谢!

发生这种情况,因为你有text/plain内部response.writeHead(200, {"Content-Type": "text/plain"}); 您需要拥有text/html

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

使用text/plain不会解析响应,您将得到纯文本(或“标签被忽略”)的内容。

使用text/html会告诉浏览器这是HTML,需要对其进行解析。

暂无
暂无

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

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