首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >没有“访问-控制-允许-源”标头错误,即使它已在SignalR服务器上配置

没有“访问-控制-允许-源”标头错误,即使它已在SignalR服务器上配置
EN

Stack Overflow用户
提问于 2018-01-01 07:58:04
回答 1查看 241关注 0票数 0

我正在尝试连接到本地机器上的一个SignalR应用程序,它运行在与一个角5应用程序不同的端口上。我在跟踪错误。我做错什么了?

未能加载 http://localhost:52527/chatHub:对飞行前请求的响应没有通过访问控制检查:请求的资源上不存在“访问控制-允许-原产地”标题。因此,'http://localhost:4200‘源不允许访问.

下面是角码。

代码语言:javascript
复制
import { Component, OnInit } from '@angular/core';
import { HubConnection } from '@aspnet/signalr-client';

@Component({
  selector: 'app-register',
  templateUrl: './register.component.html',
  styleUrls: ['./register.component.css']
})
export class RegisterComponent implements OnInit {
  private hubConnection: HubConnection;

  constructor(private http: Http) {
  }

    ngOnInit() {

          this.hubConnection = new HubConnection('http://localhost:52527/chatHub');

          console.log(this.hubConnection);
          this.hubConnection
            .start()
            .then(function (){ 
               console.log('Connection started!');
            })
            .catch(function(err) {
              console.log(err);
            } );
          }

}

以下是服务器代码。

代码语言:javascript
复制
[assembly: OwinStartup(typeof(SignalRChat.Startup))]
namespace SignalRChat
{
    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            // Any connection or hub wire up and configuration should go here

            app.Map("/signalr", map =>
            {
                map.UseCors(CorsOptions.AllowAll);
                var hubConfiguration = new HubConfiguration {};
                map.RunSignalR(hubConfiguration);
            });
        }
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-01-01 09:12:16

在Web.Config中添加了以下行,并修复了问题。

代码语言:javascript
复制
<system.webServer>
    <httpProtocol>
        <customHeaders>
            <add name="Access-Control-Allow-Origin" value="*" />
            <add name="Access-Control-Allow-Headers" value="*" />
        <add name="Access-Control-Allow-Methods" value="*" />
        </customHeaders>
    </httpProtocol>
</system.webServer>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48048051

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档