Node.js 服务器中收到的 POST 数据以未定义形式返回给客户端

发布于 2024-11-19 09:35:18 字数 948 浏览 0 评论 0原文

我开始学习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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

故事未完 2024-11-26 09:35:18

您需要查看 end 回调中的 POST.text。执行 console.dir POST 而不是 console.log POST,并查看 POST 对象是否定义了名为 text 的属性。我的猜测是事实并非如此。如果不是,请记录原始 postData 字符串并查看它是否不是您所期望的。

You need to look at POST.text inside the end callback. Instead of console.log POST, do console.dir POST and see if the POST object has a property named text defined. My guess is it does not. If not, log the raw postData string and see if it is not what you expect.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文