javascript - 在node.js中使用res()时标签被忽略

标签 javascript node.js

我找不到任何关于此的信息,所以我想我会将其发布在这里。我是 node.js 的新手

我尝试使用 Node 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>

有什么想法为什么会发生这种情况吗?谢谢!

最佳答案

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

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

使用text/plain响应不会被解析,你会得到你所提供的纯文本(或“标签被忽略”)。

使用text/html将告诉浏览器这是HTML并且需要解析。

关于javascript - 在node.js中使用res()时标签被忽略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29339929/

相关文章:

node.js - 有关异步编程实践的阅读 Material

javascript - Mac OSx 使用 sh 将文本插入到创建的文件中

javascript - Javascript 中的自然流量与付费流量

javascript - Bootstrap Tooltips,如何使用fixed with with alignment?

javascript - AngularJS 简单 ng-controller 错误

node.js - Firebase 存储-如何使用 node.js 从存储中删除文件?

node.js - Marklogic : "highlight" seem not work withe Node. js 和 QueryBuilder

javascript - 删除 this 绑定(bind)到处理程序的 jQuery 事件

javascript - 如何使用 Ramda.js 中的回调函数动态填充/扩展二维数组

javascript - 为什么我的 POST 请求不断加载?