【发布时间】:2020-03-07 22:38:29
【问题描述】:
我正在使用 express Node 后端和前端进行 Angular。问题是当我将我的 API 集成到 Angular 时,我遇到了 CORS 问题。在开发人员控制台中,我遇到了这样的错误“XmlHttpRequet cannot load http://localhost:3000/student No Access-Control-Allow-Origin”
我尝试代理后端,在 Angular 项目中添加了 proxy-confg.json。但结果也一样。仍然有 CORS 问题。
// node js endpoint
ApienpointUrl = 'http://localhost:4200/stdApi/student';
// getting data from endpoint
getStudentfromMock() {
return this.http.get(this.ApienpointUrl).pipe(
map((res: any) => res),
catchError(this.handleError)
);
}
这里是 Angular 项目 proxy.conf.json 中的代理配置
{
"/stdApi/*": {
"target": "http://localhost:3000",
"secure": false,
"logLevel": "debug",
"changeOrigin": true
}
}
我收到这样的错误:-
"XmlHttpRequet cannot load http://localhost:4200/stdApi/student No Access-Control-Allow-Origin. header is present on the request resources origin 'http://localhost:3000' therefor not allowed access".
【问题讨论】:
-
验证好 Api Endpoint url ,也许你有更多前缀(如 stdApi/v1 ),作为建议,验证你的 environement.ts 中的 apiEndpoint :而不是 'localhost:3000' 应该是'localhost:3000/stdApi' ;)
标签: angular typescript angular7