SignalR: Uncaught TypeError: Cannot read property

2020-03-30 04:02发布

VS 2013 WebForms template with bootstrap. And I've got

<script src="/Scripts/jquery-2.0.3.min.js" type="text/javascript"></script>
<script src="/Scripts/jquery.signalR-2.0.0.min.js" type="text/javascript"></script>
<script type="text/javascript" src="/signalr/hubs"></script>

( used to try signalr/hubs and ResolveClientUrl("~/signalr/hubs") etc... )

and

<script type="text/javascript">
    $(function () {
        // Proxy created on the fly connection
        var chat = $.connection.chat;

I used to try various variants and used to search a lot about this trouble but still can't find solution for now. When I check loaded page sources and click on signalr/hubs I can see proxies loaded there and I also can see chat there.

But I still receive Uncaught TypeError: Cannot read property 'chat' of undefined

  • How can I debug it?
  • Why could this happen?

1条回答
▲ chillily
2楼-- · 2020-03-30 04:57

If $.connection is not set, then the SignalR initialization code in jquery.signalR-*.min.js is not running or at least not finishing as expected. A possible cause might be having multiple jQuery versions included on the page (the VS templates add a reference, so if you added another one manually, this might be the problem). In that case, the window.$ instance which SignalR set itself up in is simply being replaced by the second jQuery initialization script which sets window.$ again. If that is not it, you should try replacing the minified SignalR script with the unminified version. Then set breakpoints at the beginning of the code (e.g. on the first line with "use strict" within the anonymous function), and at a line that says $.connection = $.signalR = signalR; or similar (towards the end).

If neither are hit, then the script is somehow not found at all. If the first one is hit, but the second one isn't, there is some other problem - you could trace it backwards from there. It might be a problem with how you create the Hub, or another configuration problem.

查看更多
登录 后发表回答