2013-01-19 14 views
2

我正在學習初學者節點簿中的教程,並且正在編寫此代碼。爲什麼node.js中的response.write不注入html?

var body = ' 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html" charset="UTF-8"/> 
</head> 

<body> 
    <form action="/upload" method="post"> 
     <textarea name="text" rows="20" cols="60"></textarea> 
     <input type="submit" value="Submit text"/> 
    </form> 
</body> 
</html>'; 
response.writeHead(200,{"Content-Type" : "text/plain"}); 
response.write(body); 
response.end(); 

我已經間隔出來,但我知道我需要把它全部在一行上,當我這樣做,因爲寫在屏幕上顯示的文字區域不顯示,在html。

我把它分開了,因爲我不確定html是否正確。

怎麼回事?

+0

_「我想我需要把它全部放在一行上「 - 是的,或者將幾個字符串連接在一起(每行一個)。你有沒有試過'「text/html」'而不是'「text/plain」'(在倒數第三行)? – nnnnnn

+0

對,我知道,但我把它隔開只是爲了防止我輸入html的問題... – praks5432

回答

16

嘗試改變這一行:

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

爲:(您想瀏覽器將其解釋爲HTML,而不是純文本)

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

+0

不錯,這正是我想要的 – praks5432

+0

但是有一個問題,如果你做以下事情: res .writeHead(200,{「Content-Type」:「text/html」}); res.write('如何渲染/加載頁面(視圖)?

'); (' http:// localhost:'+ port +'/ products'); res.write('will load(render)'); res.write(' http:// localhost:'+ port +'/ products.html page/view。'); res.end(); 它不顯示** http:**寫在文本中。 –

+0

@MuhammadHannan - 呃,爲什麼呢?您的字符串中不包含「http:」。無論如何,這似乎與這個問題無關,所以也許你應該發表一個你自己的新問題。 – nnnnnn

相關問題