Nodejs form post
Nodejs form post
我是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是怎么出现的?
当然可以!让我们一起来解决这个问题。你遇到的问题是因为在处理 request 的 data 事件时,body 变量没有被正确初始化为一个空字符串。
以下是修改后的代码:
修改后的 start 函数
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();
}
修改后的 search 函数
function search(response, request) {
let body = ''; // 初始化 body 为空字符串
request.on('data', function (data) {
body += data.toString(); // 将接收到的数据转换为字符串并追加到 body
});
request.on('end', function() {
response.writeHead(200, { "Content-Type": "text/plain" });
// 解析请求体中的查询字符串
const form = querystring.parse(body);
response.write("You’ve sent the text: " + form.DocID); // 注意 DocID 是大写的
response.end();
});
}
解释
-
初始化
body:在search函数中,我们把body初始化为空字符串let body = ''。这样可以确保每次请求开始时body都是一个干净的状态。 -
将数据转换为字符串:在
data事件的回调函数中,我们将接收到的数据data转换为字符串data.toString()并追加到body中。这是因为data通常是一个 Buffer 对象,我们需要将其转换为字符串以便进一步处理。 -
解析查询字符串:使用
querystring.parse(body)方法将请求体中的查询字符串解析成一个对象。注意,属性名是大小写敏感的,所以form.DocID而不是form.docId。
通过这些修改,你应该能够正确地获取到表单提交的数据,并且不会再看到 undefined 前缀了。希望这对你有所帮助!
body += data 相当于: body = body + data
当然是 undefinedDocID=123
var body = "" 进行一下初始化。。。。
body 设初始值
var body = '';你遇到的 undefined 问题是因为在初始化 body 变量时没有给它一个初始值。当你第一次向 body 添加数据时,由于 body 是 undefined,所以会变成 "undefinedDocID=123"。
为了修复这个问题,你需要在开始时将 body 初始化为空字符串。下面是修正后的代码:
修改后的 search 函数
function search(response, request) {
let body = ''; // 初始化 body 为一个空字符串
request.on('data', function (data) {
body += data; // 将接收到的数据追加到 body
});
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:let body = '';确保body在累加数据之前已经被正确初始化。 - 追加数据:每次接收到数据片段时,将其追加到
body中。 - 解析数据:在请求结束时使用
querystring.parse(body)解析完整的请求体,获取DocID的值。
这样修改后,你就能正确地获取到 DocID 的值,而不会出现 undefined 的情况。


