我有以下代码response.writeHead不发送POST数据
console.log(typeof(postData));
console.log(typeof(querystring.parse(postData)));
console.log(typeof(querystring.parse(postData).text));
console.log(querystring.parse(postData).text);
var stuff = querystring.parse(postData).text;
console.log(stuff);
stuff = "You've sent the text: " + stuff;
console.log(stuff);
console.log("Request handler 'upload' was called.");
response.writeHead(200, {"Content-Type": "text/plain"});
console.log(response.write(stuff));
response.end();
console.log("More text");
console.log(stuff);
在控制台中我得到
string
object
string
ffffffffffffffffffffffffffffffffffffffffffffffffff
ffffffffffffffffffffffffffffffffffffffffffffffffff
You've sent the text: ffffffffffffffffffffffffffffffffffffffffffffffffff
Request handler 'upload' was called.
false
More text
You've sent the text: ffffffffffffffffffffffffffffffffffffffffffffffffff
但网页上我看到
You've sent the text: undefined
我可以”不知道为什么我的发布数据没有发送,但我附加到的字符串是。
我正在关注The Node Beginner Book的教程。
完整requestHandlers代码(减去控制台),从哪里开始是起始页面,上传返回什么(以前的代码)
var querystring = require("querystring");
function start(response, postData) {
var body = '<html>'+
'<head>'+
'<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/html"});
response.write(body);
response.end();
}
function upload(response, postData) {
var stuff = querystring.parse(postData).text;
stuff = "You've sent the text: " + stuff;
response.writeHead(200, {"Content-Type": "text/plain"});
response.end();
}
exports.start = start;
exports.upload = upload;
您能告诉我们一个可重复的示例代码片段吗?从你所展示的看来,它看起来应该很好。 – loganfsmyth
就是这样,代码段应该可以工作。我遵循并最终从教程中复制并粘贴了整个文件。所以我认为这不是一个代码问题,而是一个配置问题,或者在我的数据中。完整的requestHandler文件看起来像我在最后发布的额外位。 – Blue
感谢您的帮助,我掏空了所有的文件,只是复制粘贴。之后,我回去发现我在主服务器文件中留下了一条额外的路线。我的猜测是,它发送的响应,然后与记录器运行的功能。所以上面的代码正在运行,但响应实际上没有使用。 – Blue