javascript - 响应中 'Access-Control-Allow-Origin' header 的值不能是 azure 函数和 javascript 中的通配符 '*'

标签 javascript c# asp.net-core cors azure-functions

<分区>

您好,我正在使用 c# azure 函数和 java 脚本。我有在本地运行的网页,也在本地运行 azure 功能。下面是我的 azure 函数,它运行在

negotiate: [POST] http://localhost:7071/api/negotiate

[FunctionName("negotiate")]
    public static async Task<SignalRConnectionInfo> Run(
        [HttpTrigger(AuthorizationLevel.Anonymous, "post")] HttpRequest req, IBinder binder,
        ILogger log)
    {
        log.LogInformation("C# HTTP trigger function processed a request.");
        var data = await Authenticated(req, log);
        var connectionInfo = binder.Bind<SignalRConnectionInfo>(new SignalRConnectionInfoAttribute { HubName = "MapUpload", UserId = data });
        return connectionInfo;
    }

然后在local.settings.json里我配置好了

 "Host": {
    "CORS": "*"
  }

下面是我的 java 脚本代码。

 <script>
   const connection = new signalR.HubConnectionBuilder()
   .withUrl('http://localhost:7071/api/')
   .configureLogging(signalR.LogLevel.Information)
   .build();

   connection.on('MapUpload', productOrdered);
   connection.onclose(() => console.log('disconnected'));

   console.log('connecting...');
   connection.start()
   .then(() => data.ready = true)
   .catch(console.error);
</script>

当我运行我的 javascript 应用程序时,出现以下错误

从来源“http://localhost:3872”访问位于“http://localhost:7071/api/negotiate”的 XMLHttpRequest 已被 CORS 策略阻止:对预检请求的响应未通过访问控制检查:当请求的凭据模式为“include”时,响应中“Access-Control-Allow-Origin” header 的值不得为通配符“*”。 XMLHttpRequest 发起的请求的凭据模式由 withCredentials 属性控制。

我添加了 "CORS": "*"但仍然出现上述错误。所以我可以知道上面的代码中是否缺少配置。有人可以帮助他们理解这个问题吗?任何帮助将不胜感激。谢谢

最佳答案

根据 fetch spec , 要使用 credentialscors 模式,你需要设置

  • Access-Control-Allow-Credentialstrue
  • Access-Control-Allow-Origin 不应设置为 *

翻译成 local.settings.json ,

 "Host": {
    "CORS": "http://localhost:3872",
    "CORSCredentials":true
  }

关于javascript - 响应中 'Access-Control-Allow-Origin' header 的值不能是 azure 函数和 javascript 中的通配符 '*',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63644356/

相关文章:

javascript - js 正则表达式清理除数字之外的所有内容

javascript - AngularJS/Rails 回形针文件上传

javascript - 沿圆形路径的 jQuery 动画

javascript - Jest 无法解析带有 @ (at) 字符的导入

c# - 获取 12154 : TNS:could not resolve the connect identifier specified error with . 网络

C# OpenXml Excel Creation - 我可以将它设置为 "Format as table"吗?

c# - 如何将虚拟路径映射到物理路径?

asp.net - Net Core 2.0寻找appsettings.Production.json

c# - 返回匿名类型的结果?

asp.net-core - 将 JWT token 存储在内存中是一个好习惯吗