gpt4 book ai didi

javascript - 在 Meteor (JavaScript) 中指定内容类型

转载 作者:可可西里 更新时间:2023-11-01 15:27:04 25 4
gpt4 key购买 nike

如何在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com