我对此一无所获,所以我想我会在这里发布。我是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,需要对其进行解析。

10-04 22:16
查看更多