【问题标题】:Send data in Meteor HTTP GET request在 Meteor HTTP GET 请求中发送数据
【发布时间】:2016-08-04 18:31:10
【问题描述】:

快速提问: 如何使用 Iron 服务器端路由将数据发送到 GET 请求?

Router.route( "/api/test", function() {
  this.response.writeHead(200, {
    'Access-Control-Allow-Origin': '*'
  });
  this.response.statusCode = 200;
  this.response.data = {test: 'test'};
  this.response.end('end');
}, {where: 'server'});

【问题讨论】:

    标签: javascript http meteor iron-router


    【解决方案1】:

    请参阅MeteorChef 网站的发送响应部分下的 2 个示例,

    Router.route( "users/:name/profile", function() {
      var name   = this.params.name,
          query  = this.request.query,
          fields = {};
    
      fields[ query.field ] = query.field;
    
      var getUser = Meteor.users.findOne( { "profile.username": name }, { fields: fields } ); 
    
      if ( getUser ) {
          this.response.statusCode = 200;
          this.response.end( getUser.profile );
      } else {
          this.response.statusCode = 404;
          this.response.end( { status: "404", message: "User not found." } );
      }
    }, { where: "server" });
    

    所以您可以像这样使用this.response.end 发送您的数据,

    Router.route( "/api/test", function() {
      this.response.writeHead(200, {
        'Access-Control-Allow-Origin': '*'
      });
      this.response.statusCode = 200;
      //this.response.data = {test: 'test'};
      this.response.end({ test: 'test' });
    }, {where: 'server'});
    

    我自己从未尝试过服务器端路由,所以我不确定它是否有效。

    【讨论】:

      猜你喜欢
      • 2011-05-26
      • 2022-01-18
      • 2017-04-21
      • 1970-01-01
      • 2023-03-18
      • 1970-01-01
      • 1970-01-01
      • 2021-08-13
      • 2017-07-14
      相关资源
      最近更新 更多