简体   繁体   English

如何在Azure函数的响应中添加自定义http标头

[英]How to add custom http header in response from Azure function

I am trying azure function (nodejs) with google authentication from a client side javascript app. 我正在尝试使用来自客户端javascript应用程序的google身份验证的azure功能(nodejs)。 I have set up CORS for the correct URL(ie http://localhost:8080 ). 我已经为正确的URL设置了CORS(即http:// localhost:8080 )。 But I am still getting the following error: 但我仍然收到以下错误:

Credentials flag is 'true', but the 'Access-Control-Allow-Credentials' header is ''. 凭据标志为“true”,但“Access-Control-Allow-Credentials”标头为“”。 It must be 'true' to allow credentials. 允许凭据必须为“true”。 Origin ' http://localhost:8080 ' is therefore not allowed access. 因此,不允许来源“ http:// localhost:8080 ”访问。

I have tried everywhere on the internet and spent few days to get the answers myself. 我在互联网上到处尝试过,花了几天时间自己得到答案。 It seems Azure http response needs to add this Access-Control-Allow-Credentials:true in the header. Azure http响应似乎需要在标头中添加此Access-Control-Allow-Credentials:true。 Is there a way to add custom headers? 有没有办法添加自定义标头?

Any help will be greatly appreciated. 任何帮助将不胜感激。

In a Node function you can specify additional headers as follows: 在Node函数中,您可以指定其他标头,如下所示:

module.exports = function (context, req) {
    context.res = {
        status: 200,
        body: "Hello " + req.query.name,
        headers: {
            'Content-Type': 'text/plain',
            'MyCustomHeader': 'Testing'
        }
    };
    context.done();
}

I have finally managed to get around the issue. 我终于成功解决了这个问题。 The trick is to remove all the CORS entries from Azure Functions app and handle it directly in your code. 诀窍是从Azure Functions应用程序中删除所有CORS条目,并直接在您的代码中处理它。

Thanks to the tip shared in another stackoverflow issue regarding azure app service, which worked for azure functions as well. 感谢有关azure app服务的另一个stackoverflow问题中共享的提示,该服务也适用于azure功能。

More details regarding the work around are at: 有关工作的更多细节包括:

github issue #620 github问题#620

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

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