
[英]Uncaught TypeError: Cannot read property 'fromJSON' of undefined
[英]Uncaught TypeError: Cannot read property 'hub' of undefined
我在 signalr 集線器斷開上調用 function 時遇到問題。
這是我的js參考
<script src="~/js/jquery.min.js"></script>
<script src="~/SignalR/dist/browser/signalr.min.js"></script>
cshtml文件中的腳本
var connection = new signalR.HubConnectionBuilder()
.withUrl("/chatt", {
accessTokenFactory: () => "testing"
})
.build();
connection.start().then(function () {
connection.invoke('GetConnectionId')
.then(function (connectionId) {
//alert(connectionId);
//removeConnectionID(connectionId);
})
}).catch(err => console.error(err));
$.connection.hub.disconnected(function () {
removeConnectionID();
})
async function removeConnectionID() {
await $.get("/Chat/Test") ;
}
在 $.connection.hub.disconnected 上我得到 Uncaught TypeError: Cannot read property 'hub' of undefined。 當我開始輸入 $.connection。 我根本看不到樞紐財產。
這是我的配置服務
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<mojDbContext>(options=> options.UseSqlServer(Configuration.GetConnectionString("IB150198T")));
services.AddControllersWithViews();
services.AddMvc();
services.AddDistributedMemoryCache();
services.AddSession();
services.AddSignalR();
// services.AddSingleton<IUserIdProvider, UserProvider>();
}
我在某處看到我應該引用名為“/SignalR/Hubs”之類的 js 文件,但在 signalR 的客戶端庫中找不到它
任何幫助表示贊賞!
您將 ASP.NET SignalR 與 ASP.NET 核心 Z358E3E185C2A6A355F30FD926E4 混淆了。
Your code is using ASP.NET Core SignalR until you try to access $.connection.hub
.connection.hub which was the pattern in ASP.NET SignalR.
您可以在此處查看 ASP.NET 內核 SignalR 的教程https://docs.microsoft.com/aspnet/core/tutorials/&signalr?view-visual=asp。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.