角度:无法在SignalR HubConnection中传递API url [英] Angular : Unable to pass API url in SignalR HubConnection

查看:262
本文介绍了角度:无法在SignalR HubConnection中传递API url的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Angular6中开发了一个项目。需要开发用于实时聊天的部分。为此,我在asp.net核心Web Apiproject中使用SignalR。现在,我想在Angular项目中使用此Web Api参考。

I have developed a project in Angular6. There is a requirement to develop section for live chat. For that I am using SignalR in my asp.net core Web Apiproject. Now I want to use this Web Api reference in my Angular project.

我正在使用链接。

但是在提供App.Component.ts中的Web Api URL,我得到以下错误:

But while providing the Web Api url in App.Component.ts, I am getting below error :


类'HubConnection'的构造方法是私有的,只能访问类声明中的

Constructor of class 'HubConnection' is private and only accessible within the class declaration.

App Component.ts:

import { HubConnection } from '@aspnet/signalr';
import { Message } from 'primeng/api';

export class AppComponent implements OnInit {
  public _hubConnection: HubConnection;
  msgs: Message[] = [];
  constructor() {   
  }
  ngOnInit() {
    this._hubConnection = new HubConnection('http://localhost:1874/notify'); // getting error on this line.

编辑:尝试以下代码:-

修改后的App.Component.ts:

Modified App.Component.ts :

import { HubConnection } from '@aspnet/signalr';
import * as signalR from '@aspnet/signalr';
import { Message } from 'primeng/api';

export class AppComponent implements OnInit {
  public _hubConnection: HubConnection;
  msgs: Message[] = [];
  constructor() {   
  }
  ngOnInit() {       
    this._hubConnection = new signalR.HubConnectionBuilder()
          .withUrl('http://localhost:1874/notify')
          .configureLogging(signalR.LogLevel.Information)
          .build();

错误:


无法加载
http:// localhost:1874 / notify / negotiate :对飞行前请求
的响应未通过访问控制检查:响应中
'Access-Control-Allow-Credentials'标头的值是其中$ b当请求的凭据模式为包括时,$ b必须为真。因此,不允许
来源‘ http:// localhost:4200 ’。 XMLHttpRequest发起的
凭据请求模式是
,由withCredentials属性控制。

Failed to load http://localhost:1874/notify/negotiate: Response to preflight request doesn't pass access control check: The value of the 'Access-Control-Allow-Credentials' header in the response is '' which must be 'true' when the request's credentials mode is 'include'. Origin 'http://localhost:4200' is therefore not allowed access. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.


推荐答案

他们将SignalR客户端 HubConnection 的构造函数更改为不再允许您通过客户端构造函数中的路由,并制成了 HubConnection 私有构造函数以强制执行更改。相反,您需要按如下方式使用 HubConnectionBuilder

They changed the constructors for SignalR Client HubConnection to no longer allow you to pass the route in the client constructor and made the HubConnection constructor private to enforce the change. Instead, you need to use the HubConnectionBuilder as follows:

新的HubConnectionBuilder()。 withUrl( / YOURROUTEHERE)。build();

您将需要在头文件中导入HubConnectionBuilder,如下所示:

You will want to import the HubConnectionBuilder in your header as follows:

import {HubConnection,HubConnectionBuilder} from'@ aspnet / signalr';

如果有帮助,我还刚刚发布了针对我的RxSignalR示例的更新,该示例适用于@ aspnet / signalr 1.0.2,RxJs 5.5.6和Angular6。请查看 ReactiveChat组件

In case it helps, I also just posted an update to my RxSignalR samples for @aspnet/signalr 1.0.2, RxJs 5.5.6 and Angular 6. Check out the ReactiveChat component for an example.

这篇关于角度:无法在SignalR HubConnection中传递API url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆