无法从离子应用程序连接到信号器集线器:访问 XMLHttpRequest ...已被 CORS 策略阻止
Cannot connect to signalr hub from ionic app: Access to XMLHttpRequest ... has been blocked by CORS policy
我正在尝试为发送和接收信号器消息的离子应用程序开发一个简单的概念证明。我有一个非常简单的 signalr web 应用程序内置于 .net 4.5 中,它成功地从应用程序主机内连接的客户端发送和接收消息。
我现在正尝试从离子应用程序连接到此,但我收到消息
Access to XMLHttpRequest at 'http://localhost:59621/signalr/negotiate'
from origin 'http://localhost:8100' has been blocked by CORS policy:
Response to preflight request doesn't pass access control check: The
value of the 'Access-Control-Allow-Origin' header in the response must
not be the wildcard '*' when the request's credentials mode is
'include'.
尝试建立与信号集线器的连接时。
非常感谢任何帮助。
.网络代码
Startup.cs
public class Startup
{
public void Configuration(IAppBuilder app)
{
// Any connection or hub wire up and configuration should go here
app.Map("/signalr", map =>
{
map.UseCors(CorsOptions.AllowAll);
var hubConfiguration = new HubConfiguration
{
EnableDetailedErrors = true,
};
map.RunSignalR(hubConfiguration);
});
}
}
ChatHub.cs
[HubName("ChatHub")]
public class ChatHub : Hub
{
public void Send(string name, string message)
{
// Call the addNewMessageToPage method to update clients.
Clients.All.addNewMessageToPage(name, message);
}
}
离子代码
import { Component } from '@angular/core';
import { NavController, DateTime, AlertController } from 'ionic-angular';
import { HubConnection, HubConnectionBuilder } from '@aspnet/signalr';
@Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
hubConnection: HubConnection;
name: string;
message: string;
messages: string[] = [];
constructor(public navCtrl: NavController, public alertCtrl: AlertController) {
}
ionViewDidLoad() {
}
ionViewWillEnter() {
let alert = this.alertCtrl.create({
title: 'Demo',
message: 'What is your name?',
inputs: [
{
name: 'Name',
placeholder: 'Name'
}
],
buttons: [
{
text: 'Enter',
handler: data => {
this.name = data.Name;
}
}
]
});
alert.present();
this.hubConnection = new HubConnectionBuilder().withUrl('http://localhost:59621/signalr').build();
this.hubConnection
.start()
.then(() => console.log('Connection started!'))
.catch(err => {
debugger;
console.log('Error while establishing connection :(')
});
this.hubConnection.on('addNewMessageToPage', (name: string, receivedMessage: string) => {
const text = `${name}: ${receivedMessage}`;
this.messages.push(text);
});
}
sendMessage() {
this.hubConnection
.invoke('send', this.name, this.message)
.then(() => this.message = '')
.catch(err => console.error(err));
}
}
离子信息
离子:
ionic(离子 CLI):4.0.6
离子框架:离子-angular 3.9.3
@ionic/app-scripts : 3.2.1
科尔多瓦:
cordova(Cordova CLI):8.1.0
科尔多瓦平台:none
系统:
NodeJS : v8.11.0 (C:\Program Files\nodejs\node.exe)
npm:5.6.0
OS : Windows 10
环境:
ANDROID_HOME : 未设置
问题是 ionic 的信号器插件需要 .Net Core 后端。我一直在尝试使用 .Net Framework 后端。
你的代码没问题。 CORS相关的问题。所以你应该运行按照下面的步骤
- 在桌面上创建快捷方式 chrome 并重命名 no-cors(任意名称)
- 右击图标并转到属性
- 接下来将目标更改为 "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --disable-web-security --user-data-dir="D:/Chrome" 并保存。
然后 运行 在这个浏览器上绝对有效。谢谢
我正在尝试为发送和接收信号器消息的离子应用程序开发一个简单的概念证明。我有一个非常简单的 signalr web 应用程序内置于 .net 4.5 中,它成功地从应用程序主机内连接的客户端发送和接收消息。
我现在正尝试从离子应用程序连接到此,但我收到消息
Access to XMLHttpRequest at 'http://localhost:59621/signalr/negotiate' from origin 'http://localhost:8100' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'.
尝试建立与信号集线器的连接时。
非常感谢任何帮助。
.网络代码
Startup.cs
public class Startup
{
public void Configuration(IAppBuilder app)
{
// Any connection or hub wire up and configuration should go here
app.Map("/signalr", map =>
{
map.UseCors(CorsOptions.AllowAll);
var hubConfiguration = new HubConfiguration
{
EnableDetailedErrors = true,
};
map.RunSignalR(hubConfiguration);
});
}
}
ChatHub.cs
[HubName("ChatHub")]
public class ChatHub : Hub
{
public void Send(string name, string message)
{
// Call the addNewMessageToPage method to update clients.
Clients.All.addNewMessageToPage(name, message);
}
}
离子代码
import { Component } from '@angular/core';
import { NavController, DateTime, AlertController } from 'ionic-angular';
import { HubConnection, HubConnectionBuilder } from '@aspnet/signalr';
@Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
hubConnection: HubConnection;
name: string;
message: string;
messages: string[] = [];
constructor(public navCtrl: NavController, public alertCtrl: AlertController) {
}
ionViewDidLoad() {
}
ionViewWillEnter() {
let alert = this.alertCtrl.create({
title: 'Demo',
message: 'What is your name?',
inputs: [
{
name: 'Name',
placeholder: 'Name'
}
],
buttons: [
{
text: 'Enter',
handler: data => {
this.name = data.Name;
}
}
]
});
alert.present();
this.hubConnection = new HubConnectionBuilder().withUrl('http://localhost:59621/signalr').build();
this.hubConnection
.start()
.then(() => console.log('Connection started!'))
.catch(err => {
debugger;
console.log('Error while establishing connection :(')
});
this.hubConnection.on('addNewMessageToPage', (name: string, receivedMessage: string) => {
const text = `${name}: ${receivedMessage}`;
this.messages.push(text);
});
}
sendMessage() {
this.hubConnection
.invoke('send', this.name, this.message)
.then(() => this.message = '')
.catch(err => console.error(err));
}
}
离子信息
离子:
ionic(离子 CLI):4.0.6
离子框架:离子-angular 3.9.3
@ionic/app-scripts : 3.2.1
科尔多瓦:
cordova(Cordova CLI):8.1.0 科尔多瓦平台:none
系统:
NodeJS : v8.11.0 (C:\Program Files\nodejs\node.exe) npm:5.6.0 OS : Windows 10
环境:
ANDROID_HOME : 未设置
问题是 ionic 的信号器插件需要 .Net Core 后端。我一直在尝试使用 .Net Framework 后端。
你的代码没问题。 CORS相关的问题。所以你应该运行按照下面的步骤
- 在桌面上创建快捷方式 chrome 并重命名 no-cors(任意名称)
- 右击图标并转到属性
- 接下来将目标更改为 "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --disable-web-security --user-data-dir="D:/Chrome" 并保存。
然后 运行 在这个浏览器上绝对有效。谢谢