SignalR- 无法创建代理,因为它会在浏览器控制台中抛出未捕获的类型错误
SignalR- Unable to create proxy as it throws uncaught typeerror in the browser console
我正在开发一个 Asp.Net MVC 应用程序,我在其中使用 signalR 进行实时数据库通知
我正在尝试使用以下脚本创建代理
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>JobStatus</title>
@Styles.Render("~/Content/css")
@Scripts.Render("~/bundles/modernizr")
@Scripts.Render("~/bundles/jquery")
<script src="~/Scripts/jquery.signalR-2.0.1.min.js"></script>
<script src="~/signalr/hubs" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
// Proxy created on the fly
var job = $.connection.jobHub;
// Declare a function on the job hub so the server can invoke it
job.client.displayStatus = function () {
getData();
};
// Start the connection
$.connection.hub.start();
getData();
});
function getData() {
var $tbl = $('#tblJobInfo');
$.ajax({
url: '../JobInfo/',
type: 'GET',
datatype: 'json',
success: function (data) {
if (data.length > 0) {
$tbl.empty();
$tbl.append(' <tr><th>ID</th><th>Name</th><th>Last Executed Date</th><th>Status</th></tr>');
var rows = [];
for (var i = 0; i < data.length; i++) {
rows.push(' <tr><td>' + data[i].JobID + '</td><td>' + data[i].Name + '</td><td>' + data[i].LastExecutionDate.toString().substr(0,10) + '</td><td>' + data[i].Status + '</td></tr>');
}
$tbl.append(rows.join(''));
}
}
});
}
</script>
</head>
<body>
<div>
<table id="tblJobInfo" style="text-align:center;margin-left:10px"></table>
</div>
</body>
</html>
我的SignalR Hub class如下
public class JobHub : Hub
{
public static void Show()
{
IHubContext context = GlobalHost.ConnectionManager.GetHubContext<JobHub>();
context.Clients.All.displayStatus();
}
}
但出于某种原因,当我在浏览器上查看我的视图并查看控制台时,它抛出了以下错误
TypeError: $.connection is undefined
var job = $.connection.jobHub;
不知道如何处理这个
检查是否:
~/Scripts/jquery.signalR-2.0.1.min.js
可用并正确加载
你也可以从cdn中引用:
http://ajax.aspnetcdn.com/ajax/signalr/jquery.signalr-2.0.1.min.js
我正在开发一个 Asp.Net MVC 应用程序,我在其中使用 signalR 进行实时数据库通知 我正在尝试使用以下脚本创建代理
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>JobStatus</title>
@Styles.Render("~/Content/css")
@Scripts.Render("~/bundles/modernizr")
@Scripts.Render("~/bundles/jquery")
<script src="~/Scripts/jquery.signalR-2.0.1.min.js"></script>
<script src="~/signalr/hubs" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
// Proxy created on the fly
var job = $.connection.jobHub;
// Declare a function on the job hub so the server can invoke it
job.client.displayStatus = function () {
getData();
};
// Start the connection
$.connection.hub.start();
getData();
});
function getData() {
var $tbl = $('#tblJobInfo');
$.ajax({
url: '../JobInfo/',
type: 'GET',
datatype: 'json',
success: function (data) {
if (data.length > 0) {
$tbl.empty();
$tbl.append(' <tr><th>ID</th><th>Name</th><th>Last Executed Date</th><th>Status</th></tr>');
var rows = [];
for (var i = 0; i < data.length; i++) {
rows.push(' <tr><td>' + data[i].JobID + '</td><td>' + data[i].Name + '</td><td>' + data[i].LastExecutionDate.toString().substr(0,10) + '</td><td>' + data[i].Status + '</td></tr>');
}
$tbl.append(rows.join(''));
}
}
});
}
</script>
</head>
<body>
<div>
<table id="tblJobInfo" style="text-align:center;margin-left:10px"></table>
</div>
</body>
</html>
我的SignalR Hub class如下
public class JobHub : Hub
{
public static void Show()
{
IHubContext context = GlobalHost.ConnectionManager.GetHubContext<JobHub>();
context.Clients.All.displayStatus();
}
}
但出于某种原因,当我在浏览器上查看我的视图并查看控制台时,它抛出了以下错误
TypeError: $.connection is undefined
var job = $.connection.jobHub;
不知道如何处理这个
检查是否:
~/Scripts/jquery.signalR-2.0.1.min.js
可用并正确加载
你也可以从cdn中引用:
http://ajax.aspnetcdn.com/ajax/signalr/jquery.signalr-2.0.1.min.js