Javascript 如何强制Azure函数将JSON输出到浏览器
我有一个azure函数设置,源代码为:Javascript 如何强制Azure函数将JSON输出到浏览器,javascript,json,node.js,azure,azure-functions,Javascript,Json,Node.js,Azure,Azure Functions,我有一个azure函数设置,源代码为: module.exports = function(context, req) { //this is the entire source, seriously context.done(null, {favoriteNumber : 3}); }; 当我使用postman之类的工具访问它时,我会得到一个很好的JSON输出,就像我想要的: { "favoriteNumber": 3 } 问题是当我在浏览器(chrome、firefo
module.exports = function(context, req) {
//this is the entire source, seriously
context.done(null, {favoriteNumber : 3});
};
当我使用postman之类的工具访问它时,我会得到一个很好的JSON输出,就像我想要的:
{
"favoriteNumber": 3
}
问题是当我在浏览器(chrome、firefox等)中访问它时,我看到:
我如何强制azure始终给我一个json输出,而不管请求头是什么?您是否尝试过将响应对象的内容类型显式设置为application\json
module.exports = function(context, req) {
res = {
body: { favoriteNumber : 3},
headers: {
'Content-Type': 'application/json'
}
};
context.done(null, res);
};
默认情况下,为内容协商配置函数。当你调用你的函数时,Chrome会发送一个标题,比如
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
所以,它要求XML,并得到了它。非常感谢!它工作完美,它有一个语法错误,所以我编辑来修复它。你在哪里找到的,我搜索了20多分钟,只找到了.net应用程序的答案谢谢!“默认情况下,函数是为内容协商配置的”函数是否可以强制配置为使用JSON?我找不到那种背景。
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8