node js form post
发布于 11 年前 作者 itking 5685 次浏览 最后一次编辑是 8 年前

我是node js新人,有个不懂的问题,请教高人 服务器是iisnode 我尝试在form中post提交数据到处理程序,不用其它插件,无关的代码我就不贴了

var querystring = require(“querystring”) function start(response) { var body = ‘<html>’+ ’<head>’+ ’<meta http-equiv=“Content-Type” ‘+ ‘content=“text/html; charset=UTF-8” />’+ ’</head>’+ ’<body>’+ ’<form name=“form1” action=“search” method=“post”>’ + ’<input type=“text” name=“DocID” />’ + ’<input type=“submit” value=“Search” />’ + ’</form>’+ ’</body>’+ ’</html>’;

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

}

function search(response, request) { var body; request.on(‘data’, function (data) { if (data != undefined) body += data; }); request.on(‘end’, function() { response.writeHead(200, { “Content-Type”: “text/plain” }); var form = querystring.parse(body); response.write("You’ve sent the text: " + form.docId); response.end(); }); }

我在body中取到的值是:undefinedDocID=123 DocID=123才是我需要的,请问,这个undefined是怎么出现的?

3 回复

body += data 相当于: body = body + data

当然是 undefinedDocID=123

var body = "" 进行一下初始化。。。。

body 设初始值

var body = '';

嗨,真是傻了,谢谢各位!

回到顶部