1
我得到試圖通過SignalR從JavaScript客戶端調用服務器端方法,當出現以下錯誤:SignalR CORS錯誤
XMLHttpRequest cannot load http://localhost:49679/signalr/send?transport=serverSentEvents&clientProtoc…XWYPNm9Av7oD1DiEodZenKiQdpQFWUqDJ9vD0UgrYc%2FjZs21i3&connectionData=%5B%5D. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:63342' is therefore not allowed access. The response had HTTP status code 500.
我建立一個簡單的原型,這就是我在我的Startup.cs類在我的Web服務器:
using Microsoft.AspNet.SignalR;
using Microsoft.Owin;
using Microsoft.Owin.Cors;
using Owin;
[assembly: OwinStartupAttribute(typeof(WebRTCSignalServer.Web.Startup))]
namespace WebRTCSignalServer.Web
{
public partial class Startup
{
public void Configuration(IAppBuilder app)
{
//app.UseCors(CorsOptions.AllowAll);
//Add SignalR
app.Map("/signalr", map =>
{
map.UseCors(CorsOptions.AllowAll);
// configure signalR
HubConfiguration hubConfiguration = new HubConfiguration()
{
EnableJavaScriptProxies = false,
EnableDetailedErrors = true
};
//GlobalHost.HubPipeline.AddModule(new ErrorHandlingPipelineModule());
// Run the SignalR pipeline. We're not using MapSignalR
// since this branch already runs under the "/signalr"
// path.
map.RunSignalR(hubConfiguration);
});
ConfigureAuth(app);
}
}
}
這是JavaScript signalr客戶端代碼:
var connection = $.hubConnection('http://localhost:49679/signalr', { useDefaultPath: false });
console.log(connection);
connection.stateChanged(function() {
console.log('stateChanged.... state=' + connection.state);
});
connection.connectionSlow(function() {
console.log('connectionSlow.... state=' + connection.state);
});
connection.reconnecting(function() {
console.log('reconnecting.... state=' + connection.state);
});
connection.reconnected(function() {
console.log('reconnected.... state=' + connection.state);
});
connection.disconnected(function() {
console.log('disconnected.... state=' + connection.state);
});
connection.error(function (error) {
console.log('error.... state=' + connection.state);
});
var webRTCSignalServerHub = connection.createHubProxy('webrtcsignalserver');
console.log(webRTCSignalServerHub);
connection.start().done(function(){
webRTCSignalServerHub.invoke('register', 'wkst0314');
});
有人可以解釋爲什麼會發生這種情況,請問如何解決?我在SignalR文檔中使用了這個例子來啓用CORS,但它不適用於我。不知道它是否重要,但我試圖在Chrome瀏覽器: http://www.asp.net/signalr/overview/guide-to-the-api/hubs-api-guide-javascript-client#crossdomain
在此先感謝您的幫助。
我得到了完全相同的錯誤,並試圖找出我在哪裏搞砸了CORS,然後我找到了答案。原來我也有我的樞紐名稱錯誤。錯誤信息是真的令人誤解。 – Hawk
請發佈您的解決方案代碼。 – Jacob