我正在尝试连接到本地机器上的一个SignalR应用程序,它运行在与一个角5应用程序不同的端口上。我在跟踪错误。我做错什么了?
未能加载 http://localhost:52527/chatHub:对飞行前请求的响应没有通过访问控制检查:请求的资源上不存在“访问控制-允许-原产地”标题。因此,'http://localhost:4200‘源不允许访问.
下面是角码。
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);
});
}
}
}发布于 2018-01-01 09:12:16
在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>https://stackoverflow.com/questions/48048051
复制相似问题