在Node.JS中读取AJAX发布变量(使用Express) [英] Reading AJAX post variables in Node.JS (with Express)

查看:71
本文介绍了在Node.JS中读取AJAX发布变量(使用Express)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取要在我的节点应用程序中发送的有关ajax帖子的值.使用这篇文章作为指导,我有这个到目前为止:

I'm trying to get the values I'm sending for an ajax post in my node application. Using this post as a guide, I have this so far:

在节点中:

 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(以及标题或内容等任何其他值),在节点中出现此错误:

This issue is that I can't req.body.id (and any other value like title or content), I'm getting this error in node:

 TypeError: Cannot read property 'id' of undefined

但是,如果我评论这些调用,则ajax成功.我搞不清楚了.我忘了什么吗?

If I comment those calls out though, the ajax is successful. I am lost. Am I forgetting something?

推荐答案

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

The req object you have there has no body property. Have a look at http://expressjs.com/api.html#req.body:

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

This property is an object containing the parsed request body. This feature is provided by the bodyParser() middleware, though other body parsing middleware may follow this convention as well. This property defaults to {} when bodyParser() is used.

因此,您需要像这样将bodyParser中间件添加到您的快速Web应用程序中:

So, you need to add the bodyParser middleware to your express webapp like this:

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

这篇关于在Node.JS中读取AJAX发布变量(使用Express)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆