【问题标题】:Reading AJAX post variables in Node.JS (with Express)在 Node.JS 中读取 AJAX 发布变量(使用 Express)
【发布时间】:2013-02-09 03:17:37
【问题描述】:

我正在尝试获取我为节点应用程序中的 ajax 帖子发送的值。使用this post 作为指导,到目前为止我有这个:

在节点中:

 var express = require('express');
 var app = express();

 var db = require('./db');

 app.get('/sender', function(req, res) {
    res.sendfile('public/send.html');
 });

 app.post('/send_save', function(req, res) {
  console.log(req.body.id)
  console.log(req.body.title);
  console.log(req.body.content);
  res.contentType('json');
  res.send({ some: JSON.stringify({response:'json'}) });
});

app.listen(3000);

在 AJAX 方面:

$('#submit').click(function() {
            alert('clicked')
            console.log($('#guid').val())
            console.log($('#page_title').val())
            console.log($('#page-content').val())
            $.ajax({
                url: "/send_save",
                type: "POST",
                dataType: "json",
                data: {
                    id: $('#guid').val(),
                    title: $('#page_title').val(),
                    content: $('#page-content').val()
                },
                contentType: "application/json",
                cache: false,
                timeout: 5000,
                complete: function() {
                  //called when complete
                  console.log('process complete');
                },

                success: function(data) {
                  console.log(data);
                  console.log('process sucess');
               },

                error: function() {
                  console.log('process error');
                },
              });
        })

这个问题是我不能 req.body.id(以及任何其他值,如标题或内容),我在节点中收到此错误:

 TypeError: Cannot read property 'id' of undefined

如果我将这些调用注释掉,则 ajax 是成功的。我迷路了。我是不是忘记了什么?

【问题讨论】:

  • 我有同样的问题 - 尝试使用param('postVariableName')

标签: javascript jquery ajax node.js express


【解决方案1】:

您拥有的req 对象没有body 属性。看看http://expressjs.com/api.html#req.body

此属性是一个包含已解析请求正文的对象。这 功能由 bodyParser() 中间件提供,尽管其他主体 解析中间件也可以遵循这个约定。这个性质 使用 bodyParser() 时默认为 {}。

因此,您需要像这样将 bodyParser 中间件添加到您的 express webapp:

var app = express();
app.use(express.bodyParser());

【讨论】:

  • 啊,我知道我错过了一些重要的东西!但是,当我包含此内容时,我开始收到“错误请求:错误请求”响应。这有什么关系吗?我必须通过 npm 安装它吗?
  • @streetlight:我认为你不需要安装任何东西,但我不知道是什么导致了这个错误。
  • 啊,好吧,不过还是感谢您的帮助!这个应用程序几乎什么都没有,所以我不知道这个糟糕的请求是怎么回事!
【解决方案2】:

按照thejh的建议,通过包含bodyParser中间件确实解决了这个问题。

只需确保访问该答案中提供的 url 即可访问 Express 更新规范:http://expressjs.com/api.html#req.body

文档提供了这个例子(Express 4.x):

var app = require('express')();
var bodyParser = require('body-parser');
var multer = require('multer'); 

app.use(bodyParser.json()); // for parsing application/json
app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded
app.use(multer()); // for parsing multipart/form-data

app.post('/', function (req, res) {
  console.log(req.body);
  res.json(req.body);
})

为此,需要单独安装 body-parser 模块:

https://www.npmjs.com/package/body-parser

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-13
    • 2011-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-09
    • 1970-01-01
    相关资源
    最近更新 更多