【问题标题】:javascript - display html on node.jsjavascript - 在 node.js 上显示 html
【发布时间】:2017-01-18 08:46:14
【问题描述】:

我是 nodejs 的新手。我正在尝试通过 nodejs 显示 html 表单,这是我的代码:

function start(response){
	console.log("Request handler 'start' was called.");
	var body = '<html>\n'+
	'<head>\n'+
	'<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 源代码检查图像:

我的问题在哪里?

【问题讨论】:

    标签: javascript html node.js server


    【解决方案1】:
    response.writeHead(200,{"Content-Type":"text/plain"});
    

    您已经告诉浏览器您正在向它发送纯文本,因此它会将文档呈现为纯文本。

    如果您要发送 HTML,则说它是 HTML。

    Content-Type: text/html
    

    【讨论】:

      【解决方案2】:

      那是因为您提供了Content-Type

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

      它是text/plain,这意味着浏览器会将响应呈现为PLAIN TEXT,而不是HTML。

      您需要做的是设置有效内容类型以将响应呈现为HTML

      对于 HTML,正确的内容类型是 text/html。所以你的代码看起来像这样,

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

      List of content types

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-03-16
        • 2016-10-26
        • 2014-05-29
        • 1970-01-01
        • 2020-06-19
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多