如何在 Meteor 中指定内容类型?
我有一个返回 JSON 的页面,但响应 header 是 html/text
我需要将其设为 application/json
。我正在使用 iron-router
然后通过模板显示 json。我只需要更改该页面的响应 header 。
我该怎么做?
最佳答案
这是一个使用服务器端路由的简单示例:
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
,您应该会看到正确的结果。
关于javascript - 在 Meteor (JavaScript) 中指定内容类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23643723/