我有一个使用此源的 azure 函数设置:
module.exports = function(context, req) {
//this is the entire source, seriously
context.done(null, {favoriteNumber : 3});
};
当我使用像 postman 这样的工具来访问它时,我得到了一个很好的 JSON 输出,就像我想要的那样:
{
"favoriteNumber": 3
}
问题是当我在浏览器(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>
最佳答案
您是否尝试过将响应对象的 Content-Type
显式设置为 application\json
?
module.exports = function(context, req) {
res = {
body: { favoriteNumber : 3},
headers: {
'Content-Type': 'application/json'
}
};
context.done(null, res);
};
默认情况下,功能配置为内容协商。当您调用函数时,Chrome 会发送类似
的 headerAccept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
因此,它请求 XML 并返回它。
关于javascript - 如何强制Azure函数将JSON输出到浏览器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42986354/