没有 'Access-Control-Allow-Origin' header 错误,即使它已在 SignalR 服务器上配置
No 'Access-Control-Allow-Origin' header error, even though it has been configured on SignalR server
我正在尝试连接到我本地机器上的 SignalR 应用程序,运行 在与 Angular 5 应用程序不同的端口上。我收到以下错误。我做错了什么?
加载失败http://localhost:52527/chatHub: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4200'因此不允许访问。
以下是 angular 代码。
import { Component, OnInit } from '@angular/core';
import { HubConnection } from '@aspnet/signalr-client';
@Component({
selector: 'app-register',
templateUrl: './register.component.html',
styleUrls: ['./register.component.css']
})
export class RegisterComponent implements OnInit {
private hubConnection: HubConnection;
constructor(private http: Http) {
}
ngOnInit() {
this.hubConnection = new HubConnection('http://localhost:52527/chatHub');
console.log(this.hubConnection);
this.hubConnection
.start()
.then(function (){
console.log('Connection started!');
})
.catch(function(err) {
console.log(err);
} );
}
}
服务器代码如下
[assembly: OwinStartup(typeof(SignalRChat.Startup))]
namespace SignalRChat
{
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 {};
map.RunSignalR(hubConfiguration);
});
}
}
}
向 Web.Config 添加了以下几行并解决了问题。
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Headers" value="*" />
<add name="Access-Control-Allow-Methods" value="*" />
</customHeaders>
</httpProtocol>
</system.webServer>
我正在尝试连接到我本地机器上的 SignalR 应用程序,运行 在与 Angular 5 应用程序不同的端口上。我收到以下错误。我做错了什么?
加载失败http://localhost:52527/chatHub: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4200'因此不允许访问。
以下是 angular 代码。
import { Component, OnInit } from '@angular/core';
import { HubConnection } from '@aspnet/signalr-client';
@Component({
selector: 'app-register',
templateUrl: './register.component.html',
styleUrls: ['./register.component.css']
})
export class RegisterComponent implements OnInit {
private hubConnection: HubConnection;
constructor(private http: Http) {
}
ngOnInit() {
this.hubConnection = new HubConnection('http://localhost:52527/chatHub');
console.log(this.hubConnection);
this.hubConnection
.start()
.then(function (){
console.log('Connection started!');
})
.catch(function(err) {
console.log(err);
} );
}
}
服务器代码如下
[assembly: OwinStartup(typeof(SignalRChat.Startup))]
namespace SignalRChat
{
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 {};
map.RunSignalR(hubConfiguration);
});
}
}
}
向 Web.Config 添加了以下几行并解决了问题。
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Headers" value="*" />
<add name="Access-Control-Allow-Methods" value="*" />
</customHeaders>
</httpProtocol>
</system.webServer>