繁体   English   中英

如何强制Azure功能将JSON输出到浏览器

[英]How can I force Azure function to output JSON to browsers

我有一个使用此源的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>

在此输入图像描述 无论请求标头如何,我如何强制azure总是给我一个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);
};

默认情况下,为内容协商配置功能。 当您呼叫您的功能时,Chrome会发送一个标题

Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8

所以,它要求XML并将其取回。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM