如何在javascript中创建这样的结构
我是JS的新手。还有一个基本的愚蠢的怀疑。请耐心等待。我想发送一份申请表:如何在javascript中创建这样的结构,javascript,node.js,Javascript,Node.js,我是JS的新手。还有一个基本的愚蠢的怀疑。请耐心等待。我想发送一份申请表: {"user":{"username":"myuser","password":"mypass","role":"myrole"}, "organization":"org_name"} 这样,用户对象可以通过req.body.user访问,组织可以通过req.body.organization访问 但当我发送此请求时: 也就是说- { "user[username]": "myuser", "user[passw
{"user":{"username":"myuser","password":"mypass","role":"myrole"}, "organization":"org_name"}
这样,用户对象可以通过req.body.user
访问,组织可以通过req.body.organization
访问
但当我发送此请求时:
也就是说-
{
"user[username]": "myuser",
"user[password]": "mypass",
"user[role]": "myrole",
"organization": "org_name"
}
当我刚刚发送
{"user":{"username":"myuser","password":"mypass","role":"myrole"}}
然后我可以使用req.body.user
访问,但不能使用上面提到的方法。为什么会这样
现在如何发送请求,以便正确访问request.body.user
和req.body.organization
编辑:
这就是请求的发送方式。前端:余烬,后端节点/快车:
Ember.$.post("http://"+window.location.hostname+":3000/api/organizations/customer",{"user":{"username":"myuser","password":"mypass","role":"myrole"}, "organization":"org_name"},function(data){
console.log(JSON.stringify(data),null, " ");
});
接收方:
console.log(JSON.stringify(req.body,null," "));
我正在尝试创建用户,但req.body.user未定义。虽然我可以使用user[username]并继续,但这就是我现在想要做的您没有向服务器发送JSON。将对象作为
数据传入不会将其作为对象发送;它只是转换为查询字符串[]。如果要发送JSON,需要对发送的数据使用JSON.stringify,而不是接收的数据
Ember.$.post("http://"+window.location.hostname+":3000/api/organizations/customer",JSON.stringify({"user":{"username":"myuser","password":"mypass","role":"myrole"}, "organization":"org_name"}),function(data){
console.log(data);
});
您没有向服务器发送JSON。将对象作为数据传入不会将其作为对象发送;它只是转换为查询字符串[]。如果要发送JSON,需要对发送的数据使用JSON.stringify,而不是接收的数据
Ember.$.post("http://"+window.location.hostname+":3000/api/organizations/customer",JSON.stringify({"user":{"username":"myuser","password":"mypass","role":"myrole"}, "organization":"org_name"}),function(data){
console.log(data);
});
您没有向服务器发送JSON。将对象作为数据传入不会将其作为对象发送;它只是转换为查询字符串[]。如果要发送JSON,需要对发送的数据使用JSON.stringify,而不是接收的数据
Ember.$.post("http://"+window.location.hostname+":3000/api/organizations/customer",JSON.stringify({"user":{"username":"myuser","password":"mypass","role":"myrole"}, "organization":"org_name"}),function(data){
console.log(data);
});
您没有向服务器发送JSON。将对象作为数据传入不会将其作为对象发送;它只是转换为查询字符串[]。如果要发送JSON,需要对发送的数据使用JSON.stringify,而不是接收的数据
Ember.$.post("http://"+window.location.hostname+":3000/api/organizations/customer",JSON.stringify({"user":{"username":"myuser","password":"mypass","role":"myrole"}, "organization":"org_name"}),function(data){
console.log(data);
});
借助克里斯蒂安·瓦尔加对这篇文章的回答,我可以进一步深入研究。虽然这个问题还没有解决,但它让我们对如何解决这个问题有了一些见解
这就是我接下来为这样一个结构所做的。根据他的建议,我使用了JSON.stringify
,但使用的位置不同:
Ember.$.post("http://"+window.location.hostname+":3000/api/organizations/customer",{"user":JSON.stringify({"username":"myuser","password":"mypass","role":"myrole"}), "organization":"org_name"},function(data){
console.log(data);
});
在后端服务器端,我可以接收req.body.user
。但是req.body.user是字符串化的JSON。为了进一步访问这个内部JSON,我必须使用JSON.parse
虽然这是可行的,但我一直在寻找更好的解决方案,因为这种后端更改对我的前端实现有效,但对RESTClient来说失败了
我知道我需要在post请求中传递要发送的数据的contentType。我找不到contentType的$.post
参数。因此,我使用了$.ajax
调用
Ember.$.ajax({
url: "http://"+window.location.hostname+":3000/api/organizations/customer",
type:"POST",
data: JSON.stringify({user:{username:username,password:password,role:role}, organization_id:organization_id}),
contentType: "application/json; charset=utf-8",
dataType:"json",
success: function(data){
console.log(JSON.stringify(data),null, " ");
}
});
其中,用户名、密码、角色、组织id
已经分配了变量 有了克里斯蒂安·瓦尔加(Christian Varga)对这篇文章的回答,我可以进一步深入研究。虽然这个问题还没有解决,但它让我们对如何解决这个问题有了一些见解
这就是我接下来为这样一个结构所做的。根据他的建议,我使用了JSON.stringify
,但使用的位置不同:
Ember.$.post("http://"+window.location.hostname+":3000/api/organizations/customer",{"user":JSON.stringify({"username":"myuser","password":"mypass","role":"myrole"}), "organization":"org_name"},function(data){
console.log(data);
});
在后端服务器端,我可以接收req.body.user
。但是req.body.user是字符串化的JSON。为了进一步访问这个内部JSON,我必须使用JSON.parse
虽然这是可行的,但我一直在寻找更好的解决方案,因为这种后端更改对我的前端实现有效,但对RESTClient来说失败了
我知道我需要在post请求中传递要发送的数据的contentType。我找不到contentType的$.post
参数。因此,我使用了$.ajax
调用
Ember.$.ajax({
url: "http://"+window.location.hostname+":3000/api/organizations/customer",
type:"POST",
data: JSON.stringify({user:{username:username,password:password,role:role}, organization_id:organization_id}),
contentType: "application/json; charset=utf-8",
dataType:"json",
success: function(data){
console.log(JSON.stringify(data),null, " ");
}
});
其中,用户名、密码、角色、组织id
已经分配了变量 有了克里斯蒂安·瓦尔加(Christian Varga)对这篇文章的回答,我可以进一步深入研究。虽然这个问题还没有解决,但它让我们对如何解决这个问题有了一些见解
这就是我接下来为这样一个结构所做的。根据他的建议,我使用了JSON.stringify
,但使用的位置不同:
Ember.$.post("http://"+window.location.hostname+":3000/api/organizations/customer",{"user":JSON.stringify({"username":"myuser","password":"mypass","role":"myrole"}), "organization":"org_name"},function(data){
console.log(data);
});
在后端服务器端,我可以接收req.body.user
。但是req.body.user是字符串化的JSON。为了进一步访问这个内部JSON,我必须使用JSON.parse
虽然这是可行的,但我一直在寻找更好的解决方案,因为这种后端更改对我的前端实现有效,但对RESTClient来说失败了
我知道我需要在post请求中传递要发送的数据的contentType。我找不到contentType的$.post
参数。因此,我使用了$.ajax
调用
Ember.$.ajax({
url: "http://"+window.location.hostname+":3000/api/organizations/customer",
type:"POST",
data: JSON.stringify({user:{username:username,password:password,role:role}, organization_id:organization_id}),
contentType: "application/json; charset=utf-8",
dataType:"json",
success: function(data){
console.log(JSON.stringify(data),null, " ");
}
});
其中,用户名、密码、角色、组织id
已经分配了变量 有了克里斯蒂安·瓦尔加(Christian Varga)对这篇文章的回答,我可以进一步深入研究。虽然这个问题还没有解决,但它让我们对如何解决这个问题有了一些见解
这就是我接下来为这样一个结构所做的。根据他的建议,我使用了JSON.stringify
,但使用的位置不同:
Ember.$.post("http://"+window.location.hostname+":3000/api/organizations/customer",{"user":JSON.stringify({"username":"myuser","password":"mypass","role":"myrole"}), "organization":"org_name"},function(data){
console.log(data);
});
在后端服务器端,我可以接收req.body.user
。但是req.body.user是字符串化的JSON。为了进一步访问这个内部JSON,我必须使用JSON.parse
虽然这是可行的,但我一直在寻找更好的解决方案,因为这种后端更改对我的前端实现有效,但对RESTClient来说失败了
我知道我需要在post请求中传递要发送的数据的contentType。我找不到contentType的$.post
参数。因此,我使用了$.ajax
调用
Ember.$.ajax({
url: "http://"+window.location.hostname+":3000/api/organizations/customer",
type:"POST",
data: JSON.stringify({user:{username:username,password:password,role:role}, organization_id:organization_id}),
contentType: "application/json; charset=utf-8",
dataType:"json",
success: function(data){
console.log(JSON.stringify(data),null, " ");
}
});
其中用户名、密码