CNode

node js form post

Iitking发布于13 年前最后回复13 年前3 回复6210 浏览0 收藏

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

var querystring = require("querystring") function start(response) { var body = ''+ ''+ '<meta http-equiv="Content-Type" '+ 'content="text/html; charset=UTF-8" />'+ ''+ ''+ '' + '' + '' + ''+ ''+ '';

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)

Y
youxiachai#1·13 年前

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

当然是 undefinedDocID=123

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

L
leapon#2·13 年前

body 设初始值

var body = '';
I
itking#3·13 年前

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

参与回复
登录后即可参与回复。登录