2014-02-21 189 views
0

我有這個前端代碼來儘快在sql表中更新表的更新。 但我找不到解決這個問題的方法。SignalR客戶端undefined

Uncaught TypeError: Cannot read property 'client' of undefined

<script src="Scripts/jquery-1.6.4.min.js"></script> 
<script src="Scripts/jquery.signalR-2.0.2.min.js"></script> 
<script src="signalr/hubs"></script> 
<script> 
    $(function() { 
     // Proxy created on the fly 
     var job = $.connection.DataHub; 
     console.log(job); 
     // 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: '../api/values', 
      type: 'GET', 
      datatype: 'json', 
      success: function (data) { 
       if (data.length > 0) { 
        $tbl.empty(); 
        $tbl.append(' <tr><th>ID</th><th>Titulo</th>/tr>'); 
        var rows = []; 
        for (var i = 0; i < data.length; i++) { 
         rows.push(' <tr><td>' + data[i].Id + '</td><td>' + data[i].titulo + '</td></tr>'); 
        } 
        $tbl.append(rows.join('')); 
       } 
      } 
     }); 
    } 
</script> 

這又如何解決呢?

回答

2

我想問題是你沒有改變中心名稱駱駝的情況。請看下圖:

var job = $.connection.dataHub; 

如果使用HubName屬性,沒有名稱變更爲駱駝上的JavaScript客戶端:

[HubName("DataHub")] 
public class DataHub: Hub 

此外,如果你正在使用ASP.NET MVC 4或5剃刀看來,使用波浪號指應用程序的根在你的代理文件參考:

<script src="~/signalr/hubs"></script> 
+0

我現在做了這個,但同樣的問題 – Severiano

+0

@Severiano,看到我更新的答案。 – Lin

0

我們得到了同樣的錯誤,但是,在我們的例子中,我們有Windows Server 2008的IIS 7的一個部署(和它的工作不埃羅rs)和另一個(相同的應用程序,相同的代碼)在Windows Server IIS 8(引發與原始海報相同的錯誤)。

我們的解決方案是在IIS 8.

部署時修改web.config和在signalr /集線器位置部分中的「system.webServer」部分註釋(注意system.webServer被註釋)

<location path="signalr/hubs"> 
    <system.web> 
     <authorization> 
      <allow users="*"/> 
     </authorization> 
    </system.web> 
    <!--system.webServer> 
     <security> 
      <authorization> 
       <add accessType="Allow" users="*"/> 
      </authorization> 
     </security> 
    </system.webServer--> 
</location> 

這是原始的,它適用於IIS 7,但未能在IIS 8:

(注意system.webServer沒有被註釋掉)

<location path="signalr/hubs"> 
    <system.web> 
     <authorization> 
      <allow users="*"/> 
     </authorization> 
    </system.web> 
    <system.webServer> 
     <security> 
      <authorization> 
       <add accessType="Allow" users="*"/> 
      </authorization> 
     </security> 
    </system.webServer> 
</location>