0

我收到错误

XMLHttpRequest cannot load http://127.0.0.1:8099//negotiate?_=1348566460118. Origin http://localhost:49848 is not allowed by Access-Control-Allow-Origin

如果我运行下面的代码

    <!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>

    <script src="Scripts/jquery-1.8.2.js"></script>
    <script src="Scripts/jquery.signalR-0.5.3.js"></script>

</head>
<body>
    <script type="text/javascript"> 
        jQuery.support.cors = true;
        var con = $.connection("http://127.0.0.1:8099/");
        $.connection.url = "http://127.0.0.1:8099/signalr";

        con.start({ transport: 'auto', xdomain: true }, function () {
           console.log('connection started!');
       });




    </script>

</body>
</html>
4

1 回答 1

1

代码有几个问题:

1) 您缺少对 /signalr/hubs 的脚本引用。

在您的情况下,它必须来自自托管服务器。所以你需要添加:

<script src="http://127.0.0.1:8099/signalr/hubs"></script>

2)您的 con 变量未指向正确的对象。

代替

var con = $.connection("http://127.0.0.1:8099/");

它应该是

jQuery.support.cors = true;

var con = $.connection.hub;
con.url = "http://127.0.0.1:8099/signalr";

con.start(function() {
    console.log('connection started!');
});
于 2012-09-26T10:33:18.090 回答