如何在javascript中制作这样的结构

时间:2022-09-28 14:31:53

I'm new to JS. And have a basic silly doubt. Please bear with me.I want to send a request of the form:

我是JS的新手。并且有一个基本的愚蠢怀疑。请耐心等待。我想发送以下表格的请求:

{"user":{"username":"myuser","password":"mypass","role":"myrole"}, "organization":"org_name"}

such that the user object can be access by req.body.user and organisation can be accessed by req.body.organization.

这样用户对象可以被req.body.user访问,组织可以通过req.body.organization访问。

But when I'm sending this request:

但是当我发送这个请求时:

it translates to-

它转化为 -

{
 "user[username]": "myuser",
 "user[password]": "mypass",
 "user[role]": "myrole",
 "organization": "org_name"
}

When I just send

当我发送

{"user":{"username":"myuser","password":"mypass","role":"myrole"}}

then I can access using req.body.user, but not the way mentioned above. Why is this so?

然后我可以使用req.body.user访问,但不是上面提到的方式。为什么会这样?

How can I send the request now such that I can access the request.body.user and req.body.organization properly?

我现在如何发送请求,以便我可以正确访问request.body.user和req.body.organization?

EDIT:

编辑:

This is how the request is sent. Front end: ember, backend node/express:

这是请求的发送方式。前端:余烬,后端节点/快递:

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, " "); 
        });

Receiving side:

接收方:

console.log(JSON.stringify(req.body,null," "));

I am trying to create the user but req.body.user is undefined. Though I can use user[username] and proceed, but that s now how I want to do

我正在尝试创建用户,但req.body.user未定义。虽然我可以使用用户[用户名]并继续,但现在我想做的

2 个解决方案

#1


3  

You aren't sending JSON to the server. Passing an object in as data doesn't send it as an object; it simply gets converted to a query string [source]. If you want to send JSON, you need to use JSON.stringify on the data that you send, not the data you receive.

您没有将JSON发送到服务器。将对象作为数据传递不会将其作为对象发送;它只是转换为查询字符串[source]。如果要发送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); 
});

#2


0  

With the help of Christian Varga's answer to this post, I could further dive in. Though the problem was still not solved, but it gave some insight of how can that be solved.

在Christian Varga对这篇文章的回答的帮助下,我可以进一步深入研究。虽然问题仍未解决,但它提供了一些有关如何解决的见解。

This is what I did next for such a structure. As per his suggestion, I used JSON.stringify but at a different place:

这就是我接下来要做的这种结构。根据他的建议,我使用了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); 
});

On the backend server side I could then receive req.body.user. But the req.body.user was stringified JSON. To further access this inner JSON, I had to use JSON.parse.

在后端服务器端,我可以接收req.body.user。但是req.body.user是字符串化的JSON。为了进一步访问这个内部JSON,我不得不使用JSON.parse。

Though this worked, but I was looking for more better solution, since this backend change worked for my front end implementation, but was failing for RESTClient.

虽然这很有效,但我一直在寻找更好的解决方案,因为这个后端更改适用于我的前端实现,但是RESTClient失败了。

I understood I need to pass the contentType of the data to be sent in the post request. I could not find $.post argument for contentType. So then I used $.ajax call.

我知道我需要传递要在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, " "); 
                 }
        });

where the username, password, role, organization_id are already assigned variables.

其中username,password,role,organization_id已经分配了变量。

#1


3  

You aren't sending JSON to the server. Passing an object in as data doesn't send it as an object; it simply gets converted to a query string [source]. If you want to send JSON, you need to use JSON.stringify on the data that you send, not the data you receive.

您没有将JSON发送到服务器。将对象作为数据传递不会将其作为对象发送;它只是转换为查询字符串[source]。如果要发送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); 
});

#2


0  

With the help of Christian Varga's answer to this post, I could further dive in. Though the problem was still not solved, but it gave some insight of how can that be solved.

在Christian Varga对这篇文章的回答的帮助下,我可以进一步深入研究。虽然问题仍未解决,但它提供了一些有关如何解决的见解。

This is what I did next for such a structure. As per his suggestion, I used JSON.stringify but at a different place:

这就是我接下来要做的这种结构。根据他的建议,我使用了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); 
});

On the backend server side I could then receive req.body.user. But the req.body.user was stringified JSON. To further access this inner JSON, I had to use JSON.parse.

在后端服务器端,我可以接收req.body.user。但是req.body.user是字符串化的JSON。为了进一步访问这个内部JSON,我不得不使用JSON.parse。

Though this worked, but I was looking for more better solution, since this backend change worked for my front end implementation, but was failing for RESTClient.

虽然这很有效,但我一直在寻找更好的解决方案,因为这个后端更改适用于我的前端实现,但是RESTClient失败了。

I understood I need to pass the contentType of the data to be sent in the post request. I could not find $.post argument for contentType. So then I used $.ajax call.

我知道我需要传递要在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, " "); 
                 }
        });

where the username, password, role, organization_id are already assigned variables.

其中username,password,role,organization_id已经分配了变量。