Javascript 在Meteor HTTP GET请求中发送数据

Javascript 在Meteor HTTP GET请求中发送数据,javascript,http,meteor,iron-router,Javascript,Http,Meteor,Iron Router,快速问题: 如何使用iron服务器端路由发送数据以获取请求 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'); }

快速问题: 如何使用iron服务器端路由发送数据以获取请求

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'});

请参见网站发送回复部分下的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'});

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

请参阅网站“发送响应”部分下的两个示例

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'});
我自己从未尝试过服务器端路由,因此我不确定它是否有效。