Javascript SignalR $.connection 未定义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11199070/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SignalR $.connection is undefined
提问by Danny Ellis Jr.
I admit it, I don't know what I'm doing.
我承认,我不知道我在做什么。
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.
我正在尝试学习如何使用 SignalR,并且我几乎是逐字逐句地在线跟踪各种示例,但我无法粘贴未定义的 $.connection。我正在 MVC 4.0 中工作,并尝试使用 nuget 下载的 signalR js 文件或来自示例项目的文件。这些是我的脚本参考。
@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")
The scripts seem to load - I've even put alerts at the start of them which fire, but $.connection is always undefined.
脚本似乎正在加载 - 我什至在它们开始时发出警报,但 $.connection 始终未定义。
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.
在一个奇怪的事件中,我有一个不同的项目,我尝试使用不同的 jQuery 库,它使用 $. 并且该对象也始终未定义。
I'm just looking for any possible explanation of what I might be doing wrong. Thanks very much in advance.
我只是在寻找我可能做错了什么的任何可能的解释。首先十分感谢。
回答by Madushan
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
- 查询
- 信号R
- /信号/集线器
- 我的脚本.js
And of course, the @Layout.cshtml was so thoughtful to add @Scripts.Render("~/bundles/jquery")
at the bottom of the <body>
tag.
当然,@Scripts.Render("~/bundles/jquery")
在<body>
标签底部添加@Layout.cshtml 非常周到。
Here's what's happening.
这就是正在发生的事情。
In this configuration, when the page loads, it loads up all the scripts in the head tag, in the order they're specified.
在此配置中,当页面加载时,它会按照指定的顺序加载 head 标记中的所有脚本。
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.
所以它加载 JQuery,然后是 SignalR,然后设置$.connection
然后自动生成的集线器脚本$.connection.hubName
,然后设置站点的自定义脚本。
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.onload
或document.onReady
之前访问$.connection
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.
到此事件触发时,布局模板在 body 标记末尾为您添加的脚本包也会下载并执行。这个包再次有 jquery 库......它重置了 SignalR 设置的所有内容$
,现在你$.connection
不再定义了。
How to Solve It
如何解决
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.
简单修复,确保您不要两次加载 JQuery,一次在 SignalR 之前,一次在 SignalR 之后。我将所有脚本移动到捆绑包之后加载,它解决了问题。此处的最佳实践是使用模板提供的“脚本”部分。
@section scripts{
@*/* load your scripts here. You can exclude jQuery,
coz it's already in the template */ *@
}
So this is not a SignalR error at all...
所以这根本不是 SignalR 错误......
That's 2 hours of my life... I'll never get back ...
那是我生命中的两个小时......我永远不会回来......
Hope This Helps.
希望这可以帮助。
回答by Danny Ellis Jr.
I solved the problem. I know what I did but I don't know why it worked for sure.
我解决了这个问题。我知道我做了什么,但我不知道为什么它确实有效。
I was referencing the main jquery library (version 1.7.2) at the top of the _Layout.cshtml view, but was not referencing any other jquery scripts. I noticed that by default in the layout view it was loading
我在 _Layout.cshtml 视图的顶部引用了主 jquery 库(版本 1.7.2),但没有引用任何其他 jquery 脚本。我注意到默认情况下在布局视图中它正在加载
@Scripts.Render("~/bundles/jquery")
at the bottom of the view and that a scripts section was located below that.
在视图的底部,脚本部分位于其下方。
@RenderSection("scripts", required:false)
I further changed my script references as shown and put my script references shown in the question above inside a
我进一步更改了我的脚本引用,如图所示,并将上面问题中显示的脚本引用放在一个
@section scripts
{
@Scripts.Render("~/Scripts/jquery.signalR-0.5.1.js")
@Scripts.Render("~/Scripts/jquery.color.js")
@Scripts.Render("~/signalr/hubs")
@Scripts.Render("~/Scripts/blasht.signalr.js")
}
wrapper on my view so that they would be included after the bundles were loaded. I assume that I was not loading all the necessary jquery because now it works.
包装在我的视图上,以便在加载包后将它们包含在内。我假设我没有加载所有必要的 jquery,因为现在它可以工作了。
Now if I can figure out how this applies to my other projects.
现在,如果我能弄清楚这如何适用于我的其他项目。
回答by allan
The reason this works is because of the order in which the javascript files are loaded. If you try and load jquery.signalR-0.5.1.js before loading jquery, then you will face the $.connection undefined because for this to register the jquery should be loaded first. In the _Layout.cshtml template the scripts load in the order
这样做的原因是因为 javascript 文件的加载顺序。如果您尝试在加载 jquery 之前加载 jquery.signalR-0.5.1.js,那么您将面临 $.connection undefined 因为为此注册 jquery 应该首先加载。在 _Layout.cshtml 模板中,脚本按顺序加载
@Scripts.Render("~/bundles/jquery")
@RenderSection("scripts", required: false)
So inside any of your views you can load signalr under the script section and this should work fine.
因此,在您的任何视图中,您都可以在脚本部分下加载信号器,这应该可以正常工作。
回答by cpoDesign
I have had same issue. for me I have included jquery 2 times in page and later in layout. Removed one of them fixed the issue.
我有同样的问题。对我来说,我在页面中包含了 2 次 jquery,后来在布局中包含了。删除其中之一解决了问题。
Hope it helps
希望能帮助到你
回答by Zeb Rawnsley
This happened to me because I dynamically loaded the ~/signalr/hubs
script before loading the jquery.signalR-1.1.4.js
script.
这发生在我身上,因为我~/signalr/hubs
在加载脚本之前动态加载了jquery.signalR-1.1.4.js
脚本。
When dynamically loading, no error was present, however if I took the output of ~/signalr/hubs
and pasted it into my chrome dev tools, I received an error message VM1047:18 Uncaught Error: SignalR: SignalR is not loaded. Please ensure jquery.signalR-x.js is referenced before ~/signalr/hubs.
at <anonymous>:18:15
at <anonymous>:300:2
动态加载时,不存在错误,但是如果我获取输出~/signalr/hubs
并将其粘贴到我的 chrome 开发工具中,我会收到一条错误消息VM1047:18 Uncaught Error: SignalR: SignalR is not loaded. Please ensure jquery.signalR-x.js is referenced before ~/signalr/hubs.
at <anonymous>:18:15
at <anonymous>:300:2