【发布时间】:2017-06-23 14:53:29
【问题描述】:
我正在使用 Angular 2 和带有 SignalR 的 ASP.NET Core。
我的错误如下所示:
XMLHttpRequest 无法加载 http://localhost:55916/signalr//signalr/negotiate?clientProtocol=1.5&connectionData=%5B%7B%22name%22%3A%22event%22%7D%5D&_=1486394845411。 响应中“Access-Control-Allow-Origin”标头的值 当请求的凭证模式为 '包括'。因此,不允许使用 Origin 'http://localhost:8080' 使用权。发起请求的凭证模式 XMLHttpRequest 由 withCredentials 属性控制。
这是我的应用配置:
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
var signingKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(Configuration["data:secretKey"]));
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
app.UseCors(config =>
config.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin());
app.UseWebSockets();
app.UseSignalR("/signalr");
app.UseMvc();
}
我也有 Angular 2 SignalR 服务女巫
constructor(){
//setups...
this.connection = $.hubConnection(this.baseUrl + 'signalr/');
this.proxy = this.connection.createHubProxy(this.proxyName);
this.registerOnServerEvents();
this.startConnection();
}
注册服务器事件:
private registerOnServerEvents(): void {
this.proxy.on('FoodAdded', (data: any) => {
debugger;
this.foodchanged.emit(data);
});
this.proxy.on('FoodDeleted', (data: any) => {
debugger;
this.foodchanged.emit('this could be data');
});
this.proxy.on('FoodUpdated', (data: any) => {
debugger;
this.foodchanged.emit('this could be data');
});
this.proxy.on('SendMessage', (data: ChatMessage) => {
debugger;
console.log('received in SignalRService: ' + JSON.stringify(data));
this.messageReceived.emit(data);
});
this.proxy.on('newCpuValue', (data: number) => {
debugger;
this.newCpuValue.emit(data);
});
}
错误从头开始。
【问题讨论】:
标签: angularjs asp.net-core signalr