我在运行开发服务器时使用 proxy.config.json
"/api": {
"target": "http://localhost:8080",
"secure": false,
"loglevel": "debug",
"changeOrigin": true
},
"/login": {
"target": "http://localhost:8080",
"secure": false,
"loglevel": "debug",
"changeOrigin": true
}
但是在构建之后我无法使用我的 rest api 我尝试使用 environment.ts 但我收到了这个错误
Access to XMLHttpRequest at 'http://localhost:8080/login' from origin 'http://localhost:4200' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status.
任何解决方案(不在服务器端)?
首先,Angular 代理仅用于ng serve
,ng build
因为它不起作用
你有两个解决方案
如果您的后端 api 托管在网络托管下的同一台服务器上,您可以执行以下操作
return this.http.post<Client>('/api/client/profile', { id: id})
在这种情况下,如果您的网址是,{urwebsiteurl}/web
那么您的 api 应该被托管{urwebsiteurl}/web/api
然后,由于您本地运行在 4200 上并且您无法在本地进行该配置,因此您将起诉代理,例如
{
"/api/": {
"target": "https://localhost:8443/", // the address of your local api
"secure": false,
"changeOrigin": false,
"logLevel": "debug"
}
}
将 apiUrl 添加到您的 environment.ts 和 environment.prod.ts 并在您的服务中使用该值而不是使用代理
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句