【发布时间】:2025-12-27 14:05:10
【问题描述】:
我收到 Access-Control-Allow-Origin 错误。
从源“https://localhost:44322”访问“https://localhost:44301/api/XXXX/GetAllXXXX”处的 XMLHttpRequest 已被 CORS 策略阻止:对预检请求的响应未通过访问控制检查:没有“Access-Control-Allow-Origin”标头出现在请求的资源上。
下面是我传递给 api 调用的标题。
getAllItems<T>(): Observable<T> {
const options = { headers: this.getRequestHeaders() };
return this.http.get<T>(this.getAllItemUrl, options);
}
protected getRequestHeaders(): HttpHeaders {
let headers = new HttpHeaders({
'Content-Type': 'application/json',
'Accept': `application/json, text/plain, */*`,
'App-Version': '1',
});
return headers;
}
我是否缺少任何东西来连接我的 API?
【问题讨论】:
-
您需要确保您的后端发送
Access-Control-Allow-Origin标头。如果你搜索 CORS,应该有很多关于这个的在线资源 -
在这里查看我的答案,*.com/questions/53167985/… 这是比使用 CORS 更好的选择