【问题标题】:Specifying content-type in Meteor (JavaScript)在 Meteor (JavaScript) 中指定内容类型
【发布时间】:2014-07-01 20:43:15
【问题描述】:

如何在 Meteor 中指定内容类型?

我有一个返回 JSON 的页面,但响应标头是 html/text 我需要将其设为 application/json。我正在使用iron-router,然后通过模板显示json。我只需要更改该页面的响应标头即可。

我该怎么做?

【问题讨论】:

  • 如果您使用的是服务器端路由,请参阅我对this question 的回答。只需将标题更改为您需要的任何内容。
  • ^ 就是这样。如果您想为这个问题写一个答案以便我接受,那就太好了:)

标签: javascript http meteor content-type


【解决方案1】:

这是一个使用服务器端路由的简单示例:

Router.map(function() {
  this.route('jsonExample', {
    where: 'server',
    path: '/json',
    action: function() {
      var obj = {cat: 'meow', dog: 'woof'};
      var headers = {'Content-type': 'application/json'};
      this.response.writeHead(200, headers);
      this.response.end(JSON.stringify(obj));
    }
  });
});

如果您将其添加到您的应用并转到localhost:3000/json,您应该会看到正确的结果。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-02-14
    • 1970-01-01
    • 1970-01-01
    • 2010-09-15
    • 1970-01-01
    • 2011-04-11
    • 1970-01-01
    • 2019-02-19
    相关资源
    最近更新 更多