为啥 Cors 阻止我对我的 Api 的请求? [复制]
Posted
技术标签:
【中文标题】为啥 Cors 阻止我对我的 Api 的请求? [复制]【英文标题】:Why is Cors blocking my request to my Api? [duplicate]为什么 Cors 阻止我对我的 Api 的请求? [复制] 【发布时间】:2020-09-05 20:01:24 【问题描述】:我正在开发一个项目,我需要访问我开发的 Api
const endpoint = 'localhost:3000/api/v1/';
const httpOptions =
headers: new HttpHeaders(
'Content-Type': 'application/json',
),
;
@Injectable(
providedIn: 'root',
)
export class RestService
constructor(private http: HttpClient)
private extractData(res: Response)
let body = res;
return body || ;
getRequest(id: String): Observable<Request>
return this.http.get<Request>(endpoint + 'request/' + id, httpOptions);
我已经授予权限以便我可以访问它,但我收到此错误
ccess to XMLHttpRequest at 'localhost:3000/api/v1/request/5ec3fbd84fe7bf4aa8d48b39' from origin 'http://localhost:4200' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.
这里是权限:
app.use(
cors(
credentials: true,
origin: "http://localhost:4200",
)
);
找到了解决办法。端点应该是'http://localhost:3000/api/v1/'
【问题讨论】:
【参考方案1】:错误消息提到请求的无效方案。尝试将“http”添加到您的端点
const endpoint = 'http://localhost:3000/api/v1/';
【讨论】:
以上是关于为啥 Cors 阻止我对我的 Api 的请求? [复制]的主要内容,如果未能解决你的问题,请参考以下文章
来自 ASP NET Web API 的 CORS 阻止 Ajax POST 调用 - 对预检请求的响应未通过访问控制检查
为啥我对 web.api 的请求被长时间运行的控制器代码阻止?
我的请求是跨源请求吗?(heroku 上的 Django rest api,CORS 没有阻止我的请求)
跨域请求被阻止:即使在我的 API 上启用了 CORS 后也会发生;