簡體   English   中英

將 angular 中的 signalR 集線器與服務器的 asp net core 連接時出現 CORS 錯誤

[英]Getting the CORS error while connecting the signalR hub in angular with asp net core from server

我的要求是將 SignalR 與 angular 與服務器端的 aspnet 核心連接起來

但是在服務器和客戶端運行代碼時,它沒有連接並收到錯誤

CORS 策略已阻止從源“http://localhost:9888”訪問“http://localhost:5000/signal/negotiate”處的 XMLHttpRequest:否“訪問” -Control-Allow-Origin' header 出現在請求的資源上

我的代碼是

儀表板.service.ts

import { Injectable } from '@angular/core';
import * as signalR from "@aspnet/signalr"

@Injectable()
export class DashboardService {

    constructor(){
 
    }

private hubConnection: signalR.HubConnection
    public startConnection = () => {
        this.hubConnection = new signalR.HubConnectionBuilder()
                                .withUrl('http://localhost:5000/signal')
                                .build();
    
        this.hubConnection
          .start()
          .then(() => console.log('Connection started'))
          .then(() =>  console.log(`SignalR connection success! connectionId:  `)
          //this.getConnectionId())
          )
          .catch(err => console.log('Error while starting connection: ' + err))
      }

}

任何人都可以幫助我嗎

您必須在服務器端代碼的啟動文件中聲明您的客戶端 URL。 如此處所述: 如何在 ASP.NET 內核中啟用 CORS

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM