本文介绍了SignalR $ .connection未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我承认,我不知道我在做什么。

I admit it, I don't know what I'm doing.

我正在尝试学习如何使用SignalR而我正在关注各种样本在线几乎逐字逐句,我无法粘贴$ .connection未定义。我正在使用MVC 4.0并尝试使用nuget下载的signalR js文件或来自示例项目的文件。这些是我的脚本引用。

I'm attempting to learn how to use SignalR and I'm following various samples online almost verbatim and I can't get pasted $.connection being undefined. I'm working in MVC 4.0 and trying to use either nuget-downloaded signalR js files or those from sample projects. These are my script references.

@Scripts.Render("~/Scripts/jquery-1.7.2.min.js")
@Scripts.Render("~/Scripts/knockout-2.0.0.js")
@Scripts.Render("~/Scripts/jquery.signalR.js")
@Scripts.Render("~/signalr/hubs")

脚本似乎加载 - 我是甚至在它们开始时发出警报,但是$ .connection总是未定义的。

The scripts seem to load - I've even put alerts at the start of them which fire, but $.connection is always undefined.

在一个奇怪的事件中,我有一个不同的项目,我正在尝试使用不同的jQuery库,它使用$。并且该对象始终未定义。

In a bizarre turn of events, I have a different project where I'm trying to use a different jQuery library and it uses $. and that object is ALSO always undefined.

我只是想找出我可能做错的任何可能的解释。非常感谢提前。

I'm just looking for any possible explanation of what I might be doing wrong. Thanks very much in advance.

推荐答案

我今天遇到了同样的问题。

I got the same problem today.

你的答案指出我正确的方向,因为我有完全相同的脚本加载设置。

Your answer pointed me in the right direction, because I had the EXACT same setup for script loading.

我的所有脚本标签都在页面顶部(头)按以下顺序。

I had all my script tags on the top of the page (head) in the following order.


  • JQuery

  • SignalR

  • / signalr / hubs

  • my-script.js

  • JQuery
  • SignalR
  • /signalr/hubs
  • my-script.js

当然,@ Layout.cshtml非常周到,可以在<底部添加 @ Scripts.Render(〜/ bundles / jquery)正文> 代码。

And of course, the @Layout.cshtml was so thoughtful to add @Scripts.Render("~/bundles/jquery") at the bottom of the <body> tag.

以下是正在发生的事情。

在此配置中,当页面加载时,它按照指定的顺序加载head标记中的所有脚本。

In this configuration, when the page loads, it loads up all the scripts in the head tag, in the order they're specified.

因此它加载JQuery,然后SignalR,设置 $。connection 然后自动生成的集线器脚本设置 $。连接。 hubName ,然后是网站的自定义脚本。

so it loads JQuery, then SignalR, which setup $.connection then the auto-generated hubs script which sets up the $.connection.hubName,then the site's custom script.

此时您的自定义代码会运行。但在某些时候你的代码,在访问<$ c $之前等待 body.onload document.onReady c> $。connection

At this point your custom code runs. BUT your code at some point, waits for the body.onload or document.onReady before accessing the $.connection

当此事件触发时,布局模板在body标签末尾为您添加的脚本包还下载并执行。这个包再次有了jquery库....它重置了 $ 上的所有SignalR设置,现在你的 $。连接已不再定义。

By the time this event fires, the script bundle which the Layout template added for you at the end of body tag also downloads and executes. This bundle has the jquery library again.... which resets everything SignalR setup on $ and now your $.connection is not defined anymore.

如何解决

轻松修复,制作确保你没有加载JQuery两次,一次在SignalR之前,一次在SignalR之后。我将所有脚本移动到捆绑后加载并修复了问题。这里的最佳做法是使用模板提供的脚本部分。

Easy fix, make sure you don't load JQuery twice, once before SignalR and once after SignalR. I moved all my scripts to load after the bundle and it fixed the issue. Best practice here is to use the 'scripts' section provided by the template.

@section scripts{
    @*/* load your scripts here. You can exclude jQuery,
      coz it's already in the template */ *@
}

所以这根本不是一个SignalR错误...

So this is not a SignalR error at all...

这是我生命的2小时......我永远不会回来......

That's 2 hours of my life... I'll never get back ...

希望这有助于。

这篇关于SignalR $ .connection未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 02:21