简体   繁体   English

如何从Ember发送接收数据

[英]How to receive data from Ember send

How do I send data from Ember to an Express server? 如何将数据从Ember发送到Express服务器?

According to the official docs, we can do a save action in a router like this: 根据官方文档,我们可以在路由器中执行以下保存操作:

export default Ember.Route.extend({
  model(params) {
    var photo = this.get('store').createRecord('photo', {
      title: 'ni hao xin ji lu',
      owner: 'ViII',
      city: 'Dalian',
      image: 'fafa.jpg',
      star: 3,
      description: '.....'
    });

    photo.save();

On the server side, for example using Express, like this: 在服务器端,例如使用Express,如下所示:

router.post('/photos', function(req, res, next) {
  console.log(req.body)//How To Do Here
});

Indeed, the server will trigger the post event, but when I search the req console out, there is nothing about the photo data from the Ember request. 确实,服务器将触发发布事件,但是当我搜索req控制台时,关于Ember请求中的照片数据一无所获。

Did I forgot something? 我忘了什么吗? How do I pass data to the server with Ember such that the server can receive it? 如何使用Ember将数据传递到服务器,以便服务器可以接收数据?

I'm using the latest Ember version. 我正在使用最新的Ember版本。

By default Ember will send the data to the server as JSON. 默认情况下,Ember会将数据作为JSON发送到服务器。 You need to tell Express to parse that. 您需要告诉Express进行解析。 I believe Ember's JSONAPIAdapter sends a content-type header of application/vnd.api+json , which needs a bit of extra configuration for the bodyParser : 我相信Ember的JSONAPIAdapter发送了一个application/vnd.api+jsoncontent-type标头,它需要对bodyParser进行一些额外的配置:

var bodyParser = require('body-parser');
app.use(bodyParser.json({type: 'application/vnd.api+json'}));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM