接收嵌套对象node.js


                    

我有一个对象:</ p>

  var val = {用户名:'gilbertbw',密码:'password',密码2:'password',电子邮件:'email@email.com',名字:'gilbert',姓氏:'BW',  test:true};
</ code> </ pre>

我对此进行了一些验证,然后将其发布到我的node.js服务器中:</ p>

  console.log(
             $ .ajax({
                 输入:“ POST”,
                 网址:“ / signup”,
                 数据:val,
                 成功:function(){console.log('数据已发布')},
                 dataType:“ json”
             })
         );
</ code> </ pre>

在服务器上,我尝试仅调用对象val:</ p>

  console.log(val.username);
</ code> </ pre>

并像往常一样将其从帖子中拉出</ p>

  val = req.param(val,null);
console.log(val.username);
</ code> </ pre>

但是两次萤火虫都吐出了500 ISE:</ p>

 “ {” error“:{” message“:”未定义val“,” stack“:” ReferenceError:未在exports.signup中定义val(C:\ Users \ Gilbert \  WebstormProjects \ NodeOfGames \outes \ user.js:37:21)
</ code> </ pre>

如果我只是 console.log(val); </ code>,它会输出null
问题所在的行是当我尝试将val.username登录到控制台时。 您如何在node.js中获取发布的对象?</ p>
     </ div>

展开原文

原文

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个回答


val </ code>是客户端变量。 服务器无法知道您将此对象命名为“ val”。 </ p>

数据:val </ code>就像:</ p>

 数据:{用户名:'gilbertbw',密码:'password',密码2:'password',电子邮件:'email@email.com',名字:'gilbert',姓氏:'BW',测试 :真正}
</ code> </ pre>

因此没有 val </ code>字段供您的服务器查找。 </ p>

改为执行此操作:</ p>

 数据:{
     “ val”:val
}
</ code> </ pre>

编辑:</ strong> </ p>

另外,您的行 req.param(val,null); </ code>应该是 req.param(“ val”,null); </ code>

val = req.body.val; </ code>

参考)</ p>
     </ div>

展开原文

原文

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)

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐