我有以下代碼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