Node.js 服务器中收到的 POST 数据以未定义形式返回给客户端
我开始学习node.js。我正在关注本教程,并且遇到了一个问题:某些 POST 数据被正确接收,但在返回时对于客户端来说,它变得“未定义”。
下面是获取 POST 数据的代码(顺便说一句,这是 Coffeescript):
postData = ""
request.setEncoding "utf8"
request.addListener "data", (postDataChunk) ->
postData += postDataChunk
console.log "Received POST data chunk '" + postDataChunk + "'."
request.addListener "end", ->
console.log "postData at end: " + postData
POST = qs.parse postData
console.log POST
route handle, pathname, response, POST.text
POST 文本与响应对象一起发送到路由函数。那里的代码是:
upload = (response, postData) ->
console.log "Request handler 'upload' was called"
console.log "post data in upload: " + postData
response.writeHead 200, "Content-Type": "text/plain"
response.end "You sent: " + postData
在控制台输出中,PostData 设置正确,但是当我在浏览器中查看输出时,它总是显示“您发送:未定义”
任何人都可以帮助我理解出了什么问题吗?
I'm starting to learn node.js. I'm following this tutorial and I've run into a problem where some POST data is received properly but when it's returned to the client, it becomes "undefined".
Here's the code for grabbing the POST data (this is Coffeescript btw):
postData = ""
request.setEncoding "utf8"
request.addListener "data", (postDataChunk) ->
postData += postDataChunk
console.log "Received POST data chunk '" + postDataChunk + "'."
request.addListener "end", ->
console.log "postData at end: " + postData
POST = qs.parse postData
console.log POST
route handle, pathname, response, POST.text
The POST text is sent to a routing function along with the response object. The code there is:
upload = (response, postData) ->
console.log "Request handler 'upload' was called"
console.log "post data in upload: " + postData
response.writeHead 200, "Content-Type": "text/plain"
response.end "You sent: " + postData
In the console output, PostData is set correctly but when I view the output in the browser it'll always say "You sent: undefined"
Can anyone help me understand what's going wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要查看
end
回调中的 POST.text。执行console.dir POST
而不是console.log POST
,并查看 POST 对象是否定义了名为text
的属性。我的猜测是事实并非如此。如果不是,请记录原始 postData 字符串并查看它是否不是您所期望的。You need to look at POST.text inside the
end
callback. Instead ofconsole.log POST
, doconsole.dir POST
and see if the POST object has a property namedtext
defined. My guess is it does not. If not, log the raw postData string and see if it is not what you expect.