未捕获的TypeError:undefined不是函数

时间:2012-11-04 08:32:29

标签: javascript jquery asp.net-mvc-4

我正在关注this site中的示例,尝试实施实时网络聊天应用程序。我是javascript的javascript新手,所以我想我只是复制一下这个例子,然后在我添加东西时学习。

这是我正在使用的代码:

@{
    ViewBag.Title = "OnlineUsers";
}


<html>
<head>
    <title>Online Users</title>
    <link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.4/leaflet.css" />
    <script src="http://cdn.leafletjs.com/leaflet-0.4/leaflet.js"></script>

</head>
<body>
    <div id="wrapper">
        <div id="upperPanel">
            <div>
                <ul id="messages" itemid="@HttpContext.Current.User.Identity.Name">

                </ul>
            </div>
            <div id="friends">

            </div>
        </div>
        <div id="bottomPanel">
            <textarea rows="2" cols="100" id="chatMessage"></textarea>
            <input id="chatSubmitMessage" type="submit" value="Send" style="margin-left: 10px;" /><br />  
        </div>
    </div>​​​​​​​​​​​​​

    <script type="text/javascript">
        $(function () {
            var pusher = new window.Pusher('0b7eeff567653e170094');
            var channel = pusher.subscribe('chat_channel');
            channel.bind('message_received', function (data) {
                $("#messages").
                append('<li>' + data.user + ' ' + data.message + ' ' + data.timestamp + '</li>');
            });

            $('#chatSubmitMessage').bind('click', function () {
                $.post("/", {
                    chatMessage: $('#chatMessage').val(),
                    username: $('#messages').attr('itemid')
                });
            });
        });
    </script>

    </div>    
</body>
</html>

此代码在Chrome调试器中出现以下错误:

Uncaught TypeError: undefined is not a function
(anonymous function)
fire jquery-1.8.2.js:974
self.add jquery-1.8.2.js:1018
jQuery.fn.jQuery.ready jquery-1.8.2.js:246
jQuery.fn.jQuery.init jquery-1.8.2.js:174
jQuery jquery-1.8.2.js:44
(anonymous function)
(anonymous function) jquery-1.8.2.js:564
jQuery.extend.globalEval jquery-1.8.2.js:565
(anonymous function) jquery-1.8.2.js:6006
jQuery.extend.each jquery-1.8.2.js:611
jQuery.fn.extend.domManip jquery-1.8.2.js:5991
jQuery.fn.extend.append jquery-1.8.2.js:5764
jQuery.fn.(anonymous function) jquery-1.8.2.js:6186
$.ajax.success jquery.mobile-1.2.0.js:3685
fire jquery-1.8.2.js:974
self.fireWith jquery-1.8.2.js:1082
done jquery-1.8.2.js:7788
callback

我假设错误与未命名的功能有关,任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:6)

您没有加载jquery,但尝试使用它。把它添加到你的脑袋

 <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>

答案 1 :(得分:2)

您是否包含了Pusher代码?确保打开Web检查器并尝试查看window.Pusher是否实际上未定义。如果未定义,include Pusher and its dependencies to the head of your file。如果那是你的问题,请告诉我。很难说出问题的内容。

相关问题