在 Meteor (JavaScript) 中指定内容类型 [英] Specifying content-type in Meteor (JavaScript)

查看:24
本文介绍了在 Meteor (JavaScript) 中指定内容类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How can I specify content-type in Meteor?

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

I've got a page that returns JSON but response header is html/text I need to make it application/json. I am using iron-router and then the json is displayed through a template. I just need to change the response header for that page.

我该怎么做?

推荐答案

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

Here's a simple example using a server-side route:

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,您应该会看到正确的结果.

If you add that to your app and go to localhost:3000/json you should see the correct result.

这篇关于在 Meteor (JavaScript) 中指定内容类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆