我在 2 个域上有网站:
- a.com
- b.com
b.com 托管 SignalR Hub,a.com 尝试创建与 b.com 的连接。
客户端JS:
var connection = $.hubConnection('http://b.com/');
var notificationHubProxy = connection.createHubProxy('notificationHub');
$.connection.hub.logging = true;
notificationHubProxy.client.resultReceived = function (result) {
console.log(result);object.
}
服务器事件中心:
public class NotificationHub : Hub
{
public void Hello()
{
Clients.All.hello();
}
public string Echo()
{
return "Notification hub is running normally";
}
}
但我总是在客户端页面上收到以下错误:
SCRIPT5007: Unable to set property 'resultReceived' of undefined or null reference.
据我检查,“notificationHubProxy.client”未定义。
哪里错了?
最佳答案
这不是跨域问题。您正在混合使用动态代理和无代理方法。 $.hubConnection
和 createHubProxy
来自无代理 API,但 .client
仅适用于动态代理。检查the documentation about both并且您将能够修复它(我不会进一步使用代码,因为我不知道您真正想使用哪种方法)。
关于javascript - 创建跨域 SignalR 连接时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24729175/