[英]How can I force Azure function to output JSON to browsers
I have an azure function setup with this source: 我有一个使用此源的azure功能设置:
module.exports = function(context, req) {
//this is the entire source, seriously
context.done(null, {favoriteNumber : 3});
};
When I use a tool like postman to visit it I get a nice JSON output, exactly like I want: 当我使用像postman这样的工具来访问它时,我得到一个不错的JSON输出,就像我想要的那样:
{
"favoriteNumber": 3
}
The problem is when I visit it in a browser (chrome, firefox, etc) I see: 问题是当我在浏览器(chrome,firefox等)中访问它时,我看到:
<ArrayOfKeyValueOfstringanyType xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/2003/10/Serialization/Arrays"><KeyValueOfstringanyType><Key>favoriteNumber</Key><Value xmlns:d3p1="http://www.w3.org/2001/XMLSchema" i:type="d3p1:int">3</Value></KeyValueOfstringanyType></ArrayOfKeyValueOfstringanyType>
How can I force azure to always give me a json output, regardless of the request headers?
无论请求标头如何,我如何强制azure总是给我一个json输出?
Have you tried setting the response object's Content-Type
explicitly to application\\json
? 您是否尝试将响应对象的
Content-Type
显式设置为application\\json
_ application\\json
?
module.exports = function(context, req) {
res = {
body: { favoriteNumber : 3},
headers: {
'Content-Type': 'application/json'
}
};
context.done(null, res);
};
By default, Functions are configured for content negotiation. 默认情况下,为内容协商配置功能。 When you call your function, Chrome is sending a header like
当您呼叫您的功能时,Chrome会发送一个标题
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
so, it's asking for XML and it gets it back. 所以,它要求XML并将其取回。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.