如何通过ajax将对象发送到节点服务器?

时间:2017-02-01 17:51:36

标签: ajax node.js body-parser

我想把一个对象发送到我的节点服务器:

cropData: { x: '0', y: '300', width: '5760', height: '3240' }

通过ajax:

$.ajax({
    type: "POST",
    url: "/",
    data: {cropData:cropData},
    success: function(data) {
        console.log('Success!')
    },
    error: function(jqXHR, textStatus, err) {
        console.log('text status '+textStatus+', err '+err)
    }
});

然后在我的server.js文件中获取:

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

当我将其记录到控制台时,我得到: 第一:  适当的对象=> {x:'0',y:'300',宽度:'5760',高度:'3240'} 第二个:  无意义的结果=>未定义

为什么会这样?首先是正确的对象,然后未定义!

我的身体传播者:

app.use(bodyParser.urlencoded({extended: true}));
我真的无法理解。请帮帮我。

0 个答案:

没有答案
相关问题