![](/img/trans.png)
[英]Error trying to connect to asp.net core signalr hub from Angular App
[英]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.