MVC4和SignalR实时数据库通知因jquery而中断
Net MVC4实时数据库通知,使用signal R。我尽了一切可能让我的应用程序退出。但是我陷入了JQuery错误中 0x800a138f-JavaScript运行时错误:无法获取属性“client” 指未定义的或空的引用 我做了所有可能的建议 我将在这里列出我的观点和代码:MVC4和SignalR实时数据库通知因jquery而中断,jquery,asp.net-mvc-4,push-notification,signalr,Jquery,Asp.net Mvc 4,Push Notification,Signalr,Net MVC4实时数据库通知,使用signal R。我尽了一切可能让我的应用程序退出。但是我陷入了JQuery错误中 0x800a138f-JavaScript运行时错误:无法获取属性“client” 指未定义的或空的引用 我做了所有可能的建议 我将在这里列出我的观点和代码: 我使用的是MVC4和SignalR版本2.1.2 Jquery版本是1.10.2 我使用的是razor视图引擎 我正在阅读一篇文章,其中我将使用部分视图获取我的DB记录 MyHub看起来像这样 public class
public class MessagesHub : Hub
{
public static void SendMessages()
{
IHubContext context = GlobalHost.ConnectionManager.GetHubContext<MessagesHub>();
context.Clients.All.updateMessages();
}
}
公共类消息shub:Hub
{
公共静态void SendMessages()
{
IHubContext context=GlobalHost.ConnectionManager.GetHubContext();
context.Clients.All.updateMessages();
}
}
我的观点是这样的
@{
ViewBag.Title = "Home Page";
}
<div class="row">
<div class="col-md-12">
<div id="messagesTable">
@ViewBag.Message="Div to include messages";
</div>
</div>
</div>
@section scripts{
<script src="~/Scripts/jquery-1.10.2.js"></script>
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script src="~/Scripts/jquery.signalR-2.1.2.js"></script>
<script src="~/Hubs/signalr/hubs"></script>
<script>
$(function () {
var notifications = $.connection.messagesHub;
//debugger;
// Create a function that the hub can call to broadcast messages.
notifications.client.updateMessages = function () {
getAllMessages()
};
// Start the connection.
$.connection.hub.start().done(function () {
alert("connection started")
getAllMessages();
}).fail(function (e) {
alert(e);
});
});
function getAllMessages() {
var tbl = $('#messagesTable');
$.ajax({
url: '/home/GetMessages',
contentType: 'application/html ; charset:utf-8',
type: 'GET',
dataType: 'html'
}).success(function (result) {
tbl.empty().append(result);
}).error(function () {
});
}
</script>
}
@{
ViewBag.Title=“主页”;
}
@ViewBag.Message=“Div以包含消息”;
@节脚本{
$(函数(){
var通知=$.connection.messagesHub;
//调试器;
//创建一个中心可以调用以广播消息的函数。
notifications.client.updateMessages=函数(){
getAllMessages()
};
//启动连接。
$.connection.hub.start().done(函数(){
警报(“连接已启动”)
getAllMessages();
}).失败(功能(e){
警报(e);
});
});
函数getAllMessages(){
var-tbl=$('#messagesTable');
$.ajax({
url:“/home/GetMessages”,
contentType:'应用程序/html;字符集:utf-8',
键入:“GET”,
数据类型:“html”
}).成功(功能(结果){
tbl.empty().append(结果);
}).错误(函数(){
});
}
}
一旦它到达查看页面,它就会抛出一个错误。我改变了我的集线器路径,给了tilde和所有东西。但我还是无法把这件事说出来。请帮帮我
错误要了我的命 我将jquery更新为1.10.2并删除了我的HUB引用,它成功了,我真的不知道是什么问题。这有点像侥幸。但不管怎样,它都起了作用。感谢@Ehsan的宝贵评论 问题就在这里:
notifications.client.updatemessages
,通知是未定义的,所以当您访问client
属性时,它会在项目的根目录中抛出errorIs Hubs文件夹?这实际上是无效的:
,在src中,我们必须给js文件引用,而您的不正确。是的,我在评论中提到过。我正确地删除和添加了,但由于signer和Jquery的版本不变,我被卡住了。使我的脚本在我的布局页面中呈现为false,然后才为true。