2012-11-04 93 views
1

我正在關注this site中的示例,嘗試實現實時Web聊天應用程序。我對jQuery非常陌生,所以我想我只是複製這個例子,並且隨着我去添加東西而學習。Uncaught TypeError:undefined不是函數

這是我使用的代碼:

@{ 
    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 

我假設的錯誤有事情做與該函數未命名,任何幫助將不勝感激。

+0

這些帖子中的每一個都針對不同的具體情況提供了答案,我認爲有訓練有素的人眼中的某個人可能能夠比瀏覽1000多個帖子更快地發現我的錯誤。 – Matt

回答

6

您未加載jquery,但嘗試使用它。將此添加到您的頭部

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

試過這個,但仍然得到相同的錯誤。 mvc 4項目不會自動包含jquery嗎? Layout.cshtml文件包含@ Scripts.Render(「〜/ bundles/jquery」,「〜/ bundles/jquerymobile」) – Matt

+0

這是畢竟的主要問題,但只是添加jQuery沒有解決它作爲其他缺少的依賴關係正如Vinay指出的那樣)也返回了同樣的錯誤。感謝您的幫助:) – Matt

2

您是否包含推式碼?確保打開Web檢查器並試圖查看window.Pusher是否實際上未定義。如果沒有定義,include Pusher and its dependencies to the head of your file。讓我知道這是否是你的問題。這個問題很難說清楚。

+0

你幫我找到了問題,因爲多個依賴不包括在內。我下載了示例解決方案,並注意到他已將它們包含在共享的佈局文件中,而不是在發佈的示例中。 +1幫助我,但牧師骨頭先回答。謝謝您的幫助 :) – Matt

相關問題