[英]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>
您是否嘗試將響應對象的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.