0x800a138f - JavaScript runtime error: Unable to get property 'PaymentHub' of undefined or null reference

0x800a138f - JavaScript runtime error: Unable to get property 'PaymentHub' of undefined or null reference

我是 ASP.NET SignalR 的新手,一直在尝试使用它开发一个简单的应用程序,实时显示对数据库所做的更改。

我在尝试 运行 应用程序时遇到以下错误:

Unhandled exception at line 52, column 13 in http://localhost:57702/ 0x800a138f - JavaScript runtime error: Unable to get property 'PaymentHub' of undefined or null reference

这是我的 javascript:

@{
    ViewBag.Title = "Status";
}

<h2>Status</h2>

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>PaymentStatus</title>
    @Styles.Render("~/Content/css")
    @Scripts.Render("~/bundles/modernizr")
    @Scripts.Render("~/bundles/jquery")
    <script src="~/Scripts/jquery.signalR-2.0.0.min.js"></script>
    <script src="~/signalr/hubs" type="text/javascript"></script>
    <script type="text/javascript">

        $(function () {

            // Proxy created on the fly
            var job = $.connection.PaymentHub;

            // 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 = $('#tblPaymentInfo');
            $.ajax({
                url: '../api/values',
                type: 'GET',
                datatype: 'json',
                success: function (data) {
                    if (data.length > 0) {
                        $tbl.empty();
                        $tbl.append(' <tr><th>ID</th><th>Payment_ID</th><th>Payment_Received</th><th>Payment_Pending</th></tr>');
                        var rows = [];
                        for (var i = 0; i < data.length; i++) {
                            rows.push(' <tr><td>' + data[i].Payment_ID + '</td><td>' + data[i].Payment_Received + '</td><td>' + data[i].Payment_Pending + '</td><td>');
                        }
                        $tbl.append(rows.join(''));
                    }
                }
            });
        }
    </script>
</head>
<body>
    <div>
        <table id="tblPaymentInfo" style="text-align:center;margin-left:10px"></table>
    </div>
</body>
</html>

我的集线器是这样的:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using Microsoft.AspNet.SignalR;
    using Microsoft.AspNet.SignalR.Hubs;

    namespace WebDatabaseUpdateSignalR
    {
        public class PaymentHub : Hub
        {
            public static void Show()
            {
                IHubContext context =       GlobalHost.ConnectionManager.GetHubContext<PaymentHub>();
                context.Clients.All.displayStatus();
            }
        }
    }

var job = $.connection.PaymentHub;应该是var job = $.connection.paymentHub;没有大写。默认情况下,在 SignalR 的 JS 部分,事物将采用驼峰式大小写。

希望这对您有所帮助。祝你好运!