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

查看:116
本文介绍了在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天全站免登陆