expressJS请求对象

时间:2011-12-02 20:29:00

标签: node.js connect express

我正试图通过expressJS tutorial。这是我的服务器代码:

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

app.use(express.bodyParser());

app.post('/', function(request, response) {
    console.log('Inside the post request!');
    console.log(request);
    response.send(resquest.body);
});

app.listen(3000);

这是我正在模拟的POST请求:

$.ajax({
    url: 'http://localhost:3000',
    type: 'POST',
    datatype: 'json',
    data: {hello: 1},
    success: function () {
        console.log('Success!');
    },
    error: function () {
        console.log('Error!');
    }
});

问题是request对象似乎不包含data: {hello: 1}。相反,它是引擎盖下参数的一大堆。我做了些蠢事吗?

3 个答案:

答案 0 :(得分:3)

我认为,由于您没有将content type设置为multipart / form-data,因此它假定为编码数据。在这种情况下,您可以将ajax请求中的数据设置为:

data: 'hello=1'

将您的内容类型设置为:application / x-www-form-urlencoded

通过request.body.hello访问它。已经有一段时间了,但试试看。

答案 1 :(得分:2)

您已撰写resquest.body而不是request.body;当你修复它时,你将能够像其他人指出的那样使用request.body.data

答案 2 :(得分:1)

您需要查看request.body。执行request.body后,您会获得{hello: 1}

相关问题