我有一个使用 SignalR 和 AspNet 的项目。我试图连接我的客户端(它是 cors),第一个请求返回 200
状态码,但我在客户端端收到这个错误:
Error during negotiation request.
我的 SignalR 服务器端类:
public class Startup1
{
public void Configuration(IAppBuilder app)
{
// Branch the pipeline here for requests that start with "/signalr"
app.Map("/signalr", map =>
{
// Setup the CORS middleware to run before SignalR.
// By default this will allow all origins. You can
// configure the set of origins and/or http verbs by
// providing a cors options with a different policy.
map.UseCors(CorsOptions.AllowAll);
var hubConfiguration = new HubConfiguration
{
// You can enable JSONP by uncommenting line below.
// JSONP requests are insecure but some older browsers (and some
// versions of IE) require JSONP to work cross domain
EnableJSONP = true
};
// Run the SignalR pipeline. We're not using MapSignalR
// since this branch already runs under the "/signalr"
// path.
map.RunSignalR(hubConfiguration);
});
}
}
我的客户端js代码:
<script src="@Arbor.CVC.Common.Common.BuildServerFilePath("inc/js/jquery.signalR-2.2.3.min.js")">
</script>
<script type="text/javascript">
$(document).ready(function () {
var username = "";
var id = "";
var connection = $.hubConnection();
var contosoChatHubProxy = connection.createHubProxy('Chat');
connection.url = 'http://localhost:64585/signalr';
connection.start().done(function () {
console.error('Now connected, connection ID=' + connection.id); }).fail(function (e) {
console.error('Could not connect ' + e); });
</script>
请求返回如下结果:
Url /signalr
ConnectionToken BwSsXO+oHqBNh7kqklTWTawIR7/Do3Rc4N+48KrCNzZLB37PlP0V+DnCYgW9EguJsYcjUAf6lhqz3LNd1hqJNxGJHHWbssn4YZEZQBNqeOPC8Ex7ndJfEvEfGslEvCDI
ConnectionId 352c6a53-64b9-4b45-85ce-ae7d20b33ba9
KeepAliveTimeout 20
DisconnectTimeout 30
ConnectionTimeout 110
TryWebSockets true
ProtocolVersion 1.4
TransportConnectTimeout 5
LongPollDelay 0
但我仍然遇到了协商错误。
Access-Control-Allow-Origin
参数。 - Chase Ernst