weixin_33724046 2012-08-08 17:43 采纳率: 0%
浏览 107

接收嵌套对象node.js

I've got an object:

var val = {username:'gilbertbw',password:'password',password2:'password',email:'email@email.com',firstname:'gilbert',lastname:'BW',test:true};

I run some validation against it and then post it to my node.js server:

console.log(
            $.ajax({
                type: 'POST',
                url: '/signup',
                data: val,
                success: function(){console.log('data posted')},
                dataType: "json"
            })
        );

On the server i have tryed just calling the object val:

console.log(val.username);

and pulling it from the post like I normally would:

val = req.param(val,null);
console.log(val.username);

But both times firebug spits out the 500 ISE:

    "{"error":{"message":"val is not defined","stack":"ReferenceError: val is not defined at exports.signup (C:\\Users\\Gilbert\\WebstormProjects\\NodeOfGames\outes\\user.js:37:21)

If i just console.log(val); it prints null The line in question is when I try to log val.username to console. How do you recive a posted object in node.js?

  • 写回答

1条回答

  • weixin_33691598 2012-08-08 17:52
    关注

    val is a client-side variable. there is no way for the server to know you named this object "val".

    data: val is just like:

    data: {username:'gilbertbw',password:'password',password2:'password',email:'email@email.com',firstname:'gilbert',lastname:'BW',test:true}
    

    so there is no val field for your server to find.

    do this instead:

    data: {
        "val": val
    }
    

    edit:

    also, your line req.param(val,null); should be req.param("val",null);
    or val = req.body.val;
    (reference)

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算