我找不到任何关于此的信息,所以我想我会将其发布在这里。我是 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/