Ember:带有嵌套JSON-Payload的jQuery Post

时间:2022-09-15 11:58:24

I want to POST some JSON to my REST-Server.

我想将一些JSON发布到我的REST-Server。

The JSON looks like that:

JSON看起来像这样:

  {
    "name": "John Doe",
    "socials": {
      "facebook": {
        "uid": "1234321",
        "fbsrCookie": "sdfsdfhgsd"
      }
    }
  }

The POST-Call in Ember looks like that:

Ember的POST-Call看起来像这样:

Ember.$.post('http://localhost:3000/register', data).then(function ...

Inspecting the Message with the Developer-Tools of Firefox show me that the parameters are the following

使用Firefox的Developer-Tools检查消息向我显示参数如下

name: "John Doe"
socials[facebook][uid]: "1234321"
socials[facebook][fbsrCookie]: "sdfsdfhgsd"

Because of the Header "Content-Type" which is "application/x-www-form-urlencoded; charset=UTF-8" I cant handle with the values as objects on my node.js-backend (since its not JSON).

由于Header“Content-Type”是“application / x-www-form-urlencoded; charset = UTF-8”,我无法将值作为我的node.js-backend上的对象处理(因为它不是JSON)。

Is there a way a to read the properties from the server-(node.js)-backend? Or how can I send the payload as JSON?

有没有办法从服务器 - (node.js)-backend读取属性?或者我如何将有效负载作为JSON发送?

2 个解决方案

#1


2  

If you have a REST server that's returning json then send it application/json:

如果你有一个返回json的REST服务器,那么发送它application / json:

vara data = {
  "name": "John Doe",
  "socials": {
    "facebook": {
      "uid": "1234321",
      "fbsrCookie": "sdfsdfhgsd"
    }
  }
}

Ember.$.ajax({
  method: "post",
  contentType: 'application/json',
  data: JSON.stringify(data)
});

#2


1  

Okay, like @sdgluck mentioned, the express.js-Framework (which Im using) can also parse the form-data (url-encoded)parameters decribed in my question above. So I double checked the including of the bodyParser-middleware of express.js and changed the "extended"-Parameter to true like so:

好的,就像@sdgluck提到的那样,express.js-Framework(我正在使用)也可以解析我上面问题中描述的表单数据(url编码)参数。所以我仔细检查了express.js的bodyParser-middleware的包含,并将“extended”-Parameter更改为true,如下所示:

app.use(bodyParser.urlencoded({
  extended: true
}));

Now express.js also parse the parameters and let me access them via dot-notation (like so: req.body.socials.facebook.uid).

现在express.js也解析参数,让我通过点符号访问它们(如:req.body.socials.facebook.uid)。

Thanks to @sdgluck !

感谢@sdgluck!

#1


2  

If you have a REST server that's returning json then send it application/json:

如果你有一个返回json的REST服务器,那么发送它application / json:

vara data = {
  "name": "John Doe",
  "socials": {
    "facebook": {
      "uid": "1234321",
      "fbsrCookie": "sdfsdfhgsd"
    }
  }
}

Ember.$.ajax({
  method: "post",
  contentType: 'application/json',
  data: JSON.stringify(data)
});

#2


1  

Okay, like @sdgluck mentioned, the express.js-Framework (which Im using) can also parse the form-data (url-encoded)parameters decribed in my question above. So I double checked the including of the bodyParser-middleware of express.js and changed the "extended"-Parameter to true like so:

好的,就像@sdgluck提到的那样,express.js-Framework(我正在使用)也可以解析我上面问题中描述的表单数据(url编码)参数。所以我仔细检查了express.js的bodyParser-middleware的包含,并将“extended”-Parameter更改为true,如下所示:

app.use(bodyParser.urlencoded({
  extended: true
}));

Now express.js also parse the parameters and let me access them via dot-notation (like so: req.body.socials.facebook.uid).

现在express.js也解析参数,让我通过点符号访问它们(如:req.body.socials.facebook.uid)。

Thanks to @sdgluck !

感谢@sdgluck!