javascript - 带有 SignalR 服务的 Azure 函数 CORS 配置不起作用
问题描述
我正在尝试从反应前端连接到 SignalR Service Azure 功能。
在前
const urlRoot = 'http://localhost:7071/api';
const connection = new HubConnectionBuilder()
.withUrl(urlRoot
)
.build();
在 Azure 功能CORS
中,我允许local.setting.json
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "",
"FUNCTIONS_WORKER_RUNTIME": "node",
"AzureSignalRConnectionString": "xxxxxx"
},
"Host": {
"CORS": "*",
"CORSCredentials": false
}
}
但是当我运行我的应用程序时,会触发一个请求http://localhost:7071/api/negotiate?negotiateVersion=1
(我不知道为什么HubConnectionBuilder()
要添加negotiateVersion=1
问题是Cors
即使在添加授权后我也会出错local.settings.json
从源“ http://localhost:3000 ”访问“ http://localhost:7071/api/negotiate?negotiateVersion=1 ”处的 XMLHttpRequest已被 CORS 策略阻止:对预检请求的响应未通过访问控制检查: 当请求的凭证模式为 'include' 时,响应中的 'Access-Control-Allow-Origin' 标头的值不能是通配符 '*'。XMLHttpRequest 发起的请求的凭证模式由 withCredentials 属性控制。
解决方案
如果在本地运行,则需要设置
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "",
"FUNCTIONS_WORKER_RUNTIME": "node",
"AzureSignalRConnectionString": "xxxxxx"
},
"Host": {
"CORS": "http://localhost:3000",
"CORSCredentials": true
}
}
您的反应应用程序在 3000 后本地运行的位置。因为不支持通配符。
推荐阅读
- laravel - Laravel:没有找到数据
- java - 有人可以解释一下 Eckel 的“On Java 8”一书中关于接口的例子吗?
- laravel - 如何连接 Laravel Lumen 和 Stackdriver
- java - 如何在 Spark 中确定最佳 shuffle 分区数
- python - 使用多线程同时打开 2 个 Webdriver
- javascript - MongoDB在NodeJS中获取字段的值
- airflow - 有没有办法设置气流 dag,如果 dag-b 仍在运行,则 dag-a 不会运行,反之亦然?
- json - 解析 JSON 数组,其中每个成员具有不同的架构但相同的一般结构
- sql-server - 我可以通过链接服务器调用 Azure 数据库中的存储过程吗?
- python - 检查两个 Python 集是否至少有一个共同元素