我对JavaScript很新.我正在尝试在JavaScript中实现一个简单的SignalR服务器回调.
首先是_Layout.cshtml
<body>
<div class="container body-content">
@RenderBody()
</div>
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/bootstrap")
@RenderSection("scripts", required: false)
</body>
接下来,我的Index.html
<body>
@section scripts
{
<script src="/Scripts/jquery-3.1.1.min.js"></script>
<script src="/Scripts/jquery.signalR-2.1.2.js"></script>
<script src="/signalr/hubs"></script>
<script type="text/javascript">
$(function () {
console.log("HELLO WORLD");
$.connection.hub.url = 'http://blah:8094/signalr';
var proxy = $.connection.DashboardHub;
proxy.client.NotifyAllClientsOfChanges = function () {
var searchUrl = "Home/GetData";
$.ajax({
url: searchUrl,
type: "POST",
success: function (data) {
$("#divData").html(data);
}
});
};
$.connection.hub.start({ transport: 'auto', xdomain: true })
.done(function () {
console.log('Connected.');
})
.fail(function (e) {
console.log('Unable to connect:' + e);
});
});
</script>
}
@using (@Html.BeginForm("Index", "Home"))
{
<div id="divData">
@{
var grid = new WebGrid(Model);
@grid.GetHtml(
mode: WebGridPagerModes.All,
columns:
grid.Columns
(
grid.Column(columnName: "SiteId", header: "Site Id", format: @<text>@item.SiteId</text>),
grid.Column(columnName: "Instrument", header: "Instrument", format: @<text>@item.InstrumentId</text> ),
.
.
.
))
}
</div>
}
</body>
我在顶部看到“HELLO WORLD”控制台日志,但我没有看到“已连接”或“无法连接”的控制台日志
我在控制台中看到一条错误,上面写着“无法读取未定义的属性’客户端’”
我跟踪了许多关于类似主题的SO帖子并尝试了很多东西.我不完全确定我的编码是否正确.
有谁看到了什么问题?
谢谢
解决方法:
不确定整个功能,但对于初学者:
var proxy = $.connection.DashboardHub;
应该
var proxy = $.connection.dashboardHub;
需要以小写字母开头