【问题标题】:node.js POST exercise displays raw HTML, not the Formnode.js POST 练习显示原始 HTML,而不是表单
【发布时间】:2013-08-11 11:16:34
【问题描述】:

我是 node.js 初学者。并遵循 Manuel K 的“Node Beginner”一书中的练习。

并成功运行练习,直到 p 上的第一个 POST 练习。 50.

因此这更像是一个浏览器配置问题:

显示表单的第一个 POST 练习是显示原始 HTML。发生在 Safari、Firefox、Chrome、Sea Monkey 中。我在网上搜索“原始 HTML 代码”,找到了一些答案,但都指向 cookie 和域。请记住,服务器位于:

 http://localhost:8888

我已经粘贴了书中的代码,所以我相当肯定“Content-Type”的东西没问题,它通过“exec”练习起作用了。

这里的“类似问题”我也查过,会继续浏览,但是……

这是相关的片段:

function start(response) {
    console.log("Request handler 'start' was called.");

    var body = '<html>'+
        '<head>'+
        '<meta http-equiv="Content-Type" content="text/html; '+
        'charset=UTF-8" />'+
        '</head>\n'+
        '<body>\n\t'+
        '<form action="/upload" method="post">\n\t'+
        '<textarea name="text" rows="20" cols="60"></textarea>\n\t'+
        '<input type="submit" value="Submit text" />\n'+
        '</form>'+
        '</body>'+
        '</html>';

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

【问题讨论】:

    标签: html forms node.js post


    【解决方案1】:

    在您的回复中更改Content-Type

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

    您在响应正文中声明了正确的 DOCTYPE,但 Content-Type 标头是浏览器用来确定如何显示响应的标头。

    【讨论】:

    • 很高兴为您提供帮助 :) Node Beginner 这本书也是我学习 Node 的地方。祝你好运!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-05
    • 1970-01-01
    • 1970-01-01
    • 2014-06-08
    • 2012-10-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多