簡體   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