Azure Functions:NodeJS - HTTP 响应呈现为 XML 而不是 HTTP 响应
Azure Functions: NodeJS - HTTP Response Renders as XML Rather than HTTP Response
我有一个用 NodeJS 编写的 Azure 函数,我试图在其中使用 302 进行 HTTP 重定向。关于响应中有效条目的文档非常少。结果,我创建了一个对象,其中我认为应该是生成重定向的正确条目,但我得到的只是一个 XML 响应。甚至像状态代码这样的项目也会显示在 XML 而不是更改真实的状态代码。
我做错了什么?
我的代码:
module.exports = function(context, req){
var url = "https://www.google.com";
context.res = {
status: 302,
headers: {
Location: url
}
}
context.done();
}
这是我在浏览器中得到的响应:
HTTP/1.1 200 OK
Cache-Control: no-cache
Pragma: no-cache
Content-Length: 1164
Content-Type: application/xml; charset=utf-8
Expires: -1
Server: Microsoft-IIS/8.0
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Wed, 18 Jan 2017 00:54:20 GMT
Connection: close
<ArrayOfKeyValueOfstringanyType xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/2003/10/Serialization/Arrays"><KeyValueOfstringanyType><Key>status</Key><Value xmlns:d3p1="http://www.w3.org/2001/XMLSchema" i:type="d3p1:int">302</Value></KeyValueOfstringanyType><KeyValueOfstringanyType><Key>headers</Key><Value i:type="ArrayOfKeyValueOfstringanyType"><KeyValueOfstringanyType><Key>Location</Key><Value xmlns:d5p1="http://www.w3.org/2001/XMLSchema" i:type="d5p1:string">https://www.google.com</Value></KeyValueOfstringanyType></Value></KeyValueOfstringanyType></ArrayOfKeyValueOfstringanyType>
问题是您没有在响应中定义 "body"。这可以设置为 null,但必须将其设置为 Azure 函数才能正确解释它。
例如将您的代码更新为:
module.exports = function(context, req){
var url = "https://www.google.com";
context.res = {
status: 302,
headers: {
Location: url
},
body : {}
}
context.done();
}
然后您将得到想要的回复:
HTTP/1.1 302 Found
Cache-Control: no-cache
Pragma: no-cache
Content-Length: 0
Expires: -1
Location: https://www.google.com
Server: Microsoft-IIS/8.0
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Wed, 18 Jan 2017 01:10:13 GMT
Connection: close
2017 年 2 月 16 日编辑 - 当前对正文使用 "null" 会在 Azure 上引发错误。结果,答案更新为使用 {}。
这是 azure 函数的错误,请参阅此 content headers issue。
目前,如果您的回复的 body 为空,解决方法是删除任何与 headers 相关的内容。
我有一个用 NodeJS 编写的 Azure 函数,我试图在其中使用 302 进行 HTTP 重定向。关于响应中有效条目的文档非常少。结果,我创建了一个对象,其中我认为应该是生成重定向的正确条目,但我得到的只是一个 XML 响应。甚至像状态代码这样的项目也会显示在 XML 而不是更改真实的状态代码。
我做错了什么?
我的代码:
module.exports = function(context, req){
var url = "https://www.google.com";
context.res = {
status: 302,
headers: {
Location: url
}
}
context.done();
}
这是我在浏览器中得到的响应:
HTTP/1.1 200 OK
Cache-Control: no-cache
Pragma: no-cache
Content-Length: 1164
Content-Type: application/xml; charset=utf-8
Expires: -1
Server: Microsoft-IIS/8.0
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Wed, 18 Jan 2017 00:54:20 GMT
Connection: close
<ArrayOfKeyValueOfstringanyType xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/2003/10/Serialization/Arrays"><KeyValueOfstringanyType><Key>status</Key><Value xmlns:d3p1="http://www.w3.org/2001/XMLSchema" i:type="d3p1:int">302</Value></KeyValueOfstringanyType><KeyValueOfstringanyType><Key>headers</Key><Value i:type="ArrayOfKeyValueOfstringanyType"><KeyValueOfstringanyType><Key>Location</Key><Value xmlns:d5p1="http://www.w3.org/2001/XMLSchema" i:type="d5p1:string">https://www.google.com</Value></KeyValueOfstringanyType></Value></KeyValueOfstringanyType></ArrayOfKeyValueOfstringanyType>
问题是您没有在响应中定义 "body"。这可以设置为 null,但必须将其设置为 Azure 函数才能正确解释它。
例如将您的代码更新为:
module.exports = function(context, req){
var url = "https://www.google.com";
context.res = {
status: 302,
headers: {
Location: url
},
body : {}
}
context.done();
}
然后您将得到想要的回复:
HTTP/1.1 302 Found
Cache-Control: no-cache
Pragma: no-cache
Content-Length: 0
Expires: -1
Location: https://www.google.com
Server: Microsoft-IIS/8.0
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Wed, 18 Jan 2017 01:10:13 GMT
Connection: close
2017 年 2 月 16 日编辑 - 当前对正文使用 "null" 会在 Azure 上引发错误。结果,答案更新为使用 {}。
这是 azure 函数的错误,请参阅此 content headers issue。
目前,如果您的回复的 body 为空,解决方法是删除任何与 headers 相关的内容。